Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(python)如果列表不为空,则访问JSON上的嵌套值,_Python_Json_Python 3.x_List_Nested - Fatal编程技术网

(python)如果列表不为空,则访问JSON上的嵌套值,

(python)如果列表不为空,则访问JSON上的嵌套值,,python,json,python-3.x,list,nested,Python,Json,Python 3.x,List,Nested,我试图访问json值,只打印不为空的“tourList” import json json_obj = { "STATUS": "SUCCESS", "DATA": { "data": [ { "destinationId": "36", "name": "Bali ", "destinationCode": "DPS", "tourList": [] },

我试图访问json值,只打印不为空的“tourList”

import json
json_obj = {
"STATUS": "SUCCESS",
"DATA": {
    "data": [
        {
            "destinationId": "36",
            "name": "Bali ",
            "destinationCode": "DPS",
            "tourList": []
        },
        {
            "destinationId": "216",
            "name": "Bandung",
            "destinationCode": "24417",
            "tourList": []
        },
        {
            "destinationId": "54",
            "name": "Batam",
            "destinationCode": "BTH",
            "tourList": [
                {
                    "tourId": "20586",
                    "tourCode": "IDBTH00585",
                    "tourName": "BATAM SPECIAL SPA PACKAGE",           
                    "tourTime": [
                        {
                            "tourStartTime": "09:00:00",
                            "tourEndTime": "16:00:00",

                        }
                    ],
                    "pricing": [
                        {
                            "adultPrice": "193.00",
                            "tourId": "20586"
                        }
                    ]
                }
            ]
        }          

    ]
 }
}

wanted = ['tourId', 'tourCode', 'tourName', 'tourTime','pricing']

for item in json_obj["DATA"]["data"]:
    details = item['tourList']
    if not details:
       print("")
    else:
        for key in wanted:
            print(key, ':', json.dumps(details[key], indent=4))
            #Put a blank line at the end of the details for each item
            print() 
然后我犯了这个错误

回溯(最近一次呼叫最后一次): 文件“testapi.py”,第57行,在 打印(键“:”,json.dumps(详细信息[key],缩进=4)) TypeError:列表索引必须是整数或片,而不是str

我认为这是一个错误,因为一些旅游名单是空的,请帮助我如何检查旅游名单是空的,然后只打印不是空的旅游名单

你能帮我把结果变成这样吗

tourId : "20586"
tourCode : "IDBTH00585"
tourName : "BATAM SPECIAL SPA PACKAGE"
tourStartTime: "09:00:00"
tourEndTime: "16:00:00"
adultPrice: "193.00"
tourId: "20586"
详细信息
项目['tourList']
)是
目录的
列表
,而不是
目录本身。改为:

for d in details:
    for key in wanted:
        print(key, ':', json.dumps(d[key], indent=4))
或者,如果您只需要所述
列表中的第一个
命令

for key in wanted:
    print(key, ':', json.dumps(details[0][key], indent=4))
详细信息
项目['tourList']
)是
目录的
列表
,而不是
目录本身。改为:

for d in details:
    for key in wanted:
        print(key, ':', json.dumps(d[key], indent=4))
或者,如果您只需要所述
列表中的第一个
命令

for key in wanted:
    print(key, ':', json.dumps(details[0][key], indent=4))
希望它可以帮助您:

data = json_obj.get("DATA")
for key in data.keys():
    for get_list in data[key]:
        if not get_list.get('tourList'):
            print('Skip it')         
        else:
            print('Do Something')
希望它可以帮助您:

data = json_obj.get("DATA")
for key in data.keys():
    for get_list in data[key]:
        if not get_list.get('tourList'):
            print('Skip it')         
        else:
            print('Do Something')

details=item['tourList'][0]在尝试访问键之前获取词典。您的
details
正在引用一个'tourList
列表,这就是为什么您的
details[key]`失败的原因,因为它需要的是列表的索引,而不是您期望的词典键。编辑:Krishna所说的.details=item['tourList'][0]在您尝试访问键之前获取词典您的
details
正在引用一个'tourList
列表,这就是为什么您的
details[key]`失败的原因,因为它需要的是列表的索引,而不是您期望的词典键。编辑:克里希纳说了什么。完成了,谢谢:)你能帮我这样做吗?tourId:“20586”tourCode:“IDBTH00585”tourName:“巴淡岛特别水疗套餐”“tourStartTime:“09:00:00”“tourEndTime:“16:00”“成人价格”:“193.00”,“tourId:“20586”这是一个新问题;)你必须找到一种方法来降低筑巢水平。完成,谢谢:)你也能帮我这样做吗?tourId:“20586”tourCode:“IDBTH00585”tourName:“巴淡岛特别水疗套餐”“tourStartTime:“09:00:00”“tourEndTime:“16:00:00”“成人价格”:“193.00”,“tourId:“20586”这是一个新问题;)你必须找到一种降低嵌套级别的方法。