python - json中,list中包含字典,字典中又包含list,如何取出json中的url属性的值?
高洛峰
高洛峰 2017-04-17 17:25:23
[Python讨论组]
{
        "id": "2016021715272727370",
        "text": "2016017号",
        "state": "open",
        "attributes": {
            "url": "scxx_presale_show.php?projid=15796&id=2016021715272727370"
        },
        "children": [
            {
                "id": "0150050078004",
                "text": "3号住宅楼",
                "state": "open",
                "attributes": {
                    "url": "scxx_floor_show.php?projid=15796&id=0150050078004"
                },
                "children": [
                    {
                        "id": "0150050078004",
                        "text": "地下室或商铺",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=00&id=0150050078004"
                        }
                    },
                    {
                        "id": "0150050078004",
                        "text": "01单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=01&id=0150050078004"
                        }
                    },
                    {
                        "id": "0150050078004",
                        "text": "02单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=02&id=0150050078004"
                        }
                    }
                ]
            }
        ]
    },
    {
        "id": "2016022309583828846",
        "text": "2016023号",
        "state": "open",
        "attributes": {
            "url": "scxx_presale_show.php?projid=15796&id=2016022309583828846"
        },
        "children": [
            {
                "id": "0150050078004",
                "text": "3号住宅楼",
                "state": "open",
                "attributes": {
                    "url": "scxx_floor_show.php?projid=15796&id=0150050078004"
                },
                "children": [
                    {
                        "id": "0150050078004",
                        "text": "地下室或商铺",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=00&id=0150050078004"
                        }
                    },
                    {
                        "id": "0150050078004",
                        "text": "01单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=01&id=0150050078004"
                        }
                    },
                    {
                        "id": "0150050078004",
                        "text": "02单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=02&id=0150050078004"
                        }
                    }
                ]
            },
            {
                "id": "0150050078005",
                "text": "5号住宅楼",
                "state": "open",
                "attributes": {
                    "url": "scxx_floor_show.php?projid=15796&id=0150050078005"
                },
                "children": [
                    {
                        "id": "0150050078005",
                        "text": "地下室或商铺",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=00&id=0150050078005"
                        }
                    },
                    {
                        "id": "0150050078005",
                        "text": "01单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=01&id=0150050078005"
                        }
                    },
                    {
                        "id": "0150050078005",
                        "text": "02单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=02&id=0150050078005"
                        }
                    }
                ]
            },
            {
                "id": "0150050078006",
                "text": "6号住宅楼",
                "state": "open",
                "attributes": {
                    "url": "scxx_floor_show.php?projid=15796&id=0150050078006"
                },
                "children": [
                    {
                        "id": "0150050078006",
                        "text": "地下室或商铺",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=00&id=0150050078006"
                        }
                    },
                    {
                        "id": "0150050078006",
                        "text": "01单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=01&id=0150050078006"
                        }
                    },
                    {
                        "id": "0150050078006",
                        "text": "02单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=02&id=0150050078006"
                        }
                    }
                ]
            },
            {
                "id": "0150050078008",
                "text": "7号住宅楼",
                "state": "open",
                "attributes": {
                    "url": "scxx_floor_show.php?projid=15796&id=0150050078008"
                },
                "children": [
                    {
                        "id": "0150050078008",
                        "text": "地下室或商铺",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=00&id=0150050078008"
                        }
                    },
                    {
                        "id": "0150050078008",
                        "text": "01单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=01&id=0150050078008"
                        }
                    },
                    {
                        "id": "0150050078008",
                        "text": "02单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=02&id=0150050078008"
                        }
                    }
                ]
            },
            {
                "id": "0150050078009",
                "text": "8号住宅楼",
                "state": "open",
                "attributes": {
                    "url": "scxx_floor_show.php?projid=15796&id=0150050078009"
                },
                "children": [
                    {
                        "id": "0150050078009",
                        "text": "地下室或商铺",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=00&id=0150050078009"
                        }
                    },
                    {
                        "id": "0150050078009",
                        "text": "01单元",
                        "attributes": {
                            "url": "scxx_subroom_show.php?sub=01&id=0150050078009"
                        }
                    }
                ]
            }
        ]
    }
}
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(3)
ringa_lee

这样直接粘贴出大段代码的问题既不利于想帮你解决问题的人查看,而且给出的代码格式上还有些问题;因此提问还是得先学会梳理问题本身,找到问题核心后再拟一个简单的模型示例后提出。

回到你的问题:整体是个json,可以先将 json 转换为 Python 的 str,再用正则提取所有url,假设你的 json 数据来自网络,可以参考如下:

import json
import urllib
import re

json_str = urllib.request.urlopen(...).decode()  #获取 json 字符串   
pattern = re.compile('"url": "(.+?)"')  #设定正则模式
result = pattern.findall(json_str)  #获得结果

测试了一下你的数据:

>>> print(result)

['scxx_presale_show.php?projid=15796&id=2016021715272727370', 'scxx_floor_show.php?projid=15796&id=0150050078004', 'scxx_subroom_show.php?sub=00&id=0150050078004', 'scxx_subroom_show.php?sub=01&id=0150050078004', 'scxx_subroom_show.php?sub=02&id=0150050078004', 'scxx_presale_show.php?projid=15796&id=2016022309583828846', 'scxx_floor_show.php?projid=15796&id=0150050078004', 'scxx_subroom_show.php?sub=00&id=0150050078004', 'scxx_subroom_show.php?sub=01&id=0150050078004', 'scxx_subroom_show.php?sub=02&id=0150050078004', 'scxx_floor_show.php?projid=15796&id=0150050078005', 'scxx_subroom_show.php?sub=00&id=0150050078005', 'scxx_subroom_show.php?sub=01&id=0150050078005', 'scxx_subroom_show.php?sub=02&id=0150050078005', 'scxx_floor_show.php?projid=15796&id=0150050078006', 'scxx_subroom_show.php?sub=00&id=0150050078006', 'scxx_subroom_show.php?sub=01&id=0150050078006', 'scxx_subroom_show.php?sub=02&id=0150050078006', 'scxx_floor_show.php?projid=15796&id=0150050078008', 'scxx_subroom_show.php?sub=00&id=0150050078008', 'scxx_subroom_show.php?sub=01&id=0150050078008', 'scxx_subroom_show.php?sub=02&id=0150050078008', 'scxx_floor_show.php?projid=15796&id=0150050078009', 'scxx_subroom_show.php?sub=00&id=0150050078009', 'scxx_subroom_show.php?sub=01&id=0150050078009']
黄舟

我猜你的意思是不确定是否有嵌套的children属性?

如果不是,打回去重新提问。

PHP中文网

难道不能用json.loads(str)?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号