首页 > json中,list中包含字典,字典中又包含list,如何取出json中的url属性的值?

json中,list中包含字典,字典中又包含list,如何取出json中的url属性的值?

{
        "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"
                        }
                    }
                ]
            }
        ]
    }
}

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

回到你的问题:整体是个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属性?

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


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

【热门文章】
【热门文章】