我试着去访问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(细节[关键],缩进= 4))类型错误:列表索引必须为整数或切片,不str的
即时通讯思想的错误,因为一些tourList是空的,帮我如何检查tourList是空的,然后只打印tourList不是空
也可以帮我,这样的结果是这样的
tourId : "20586"
tourCode : "IDBTH00585"
tourName : "BATAM SPECIAL SPA PACKAGE"
tourStartTime: "09:00:00"
tourEndTime: "16:00:00"
adultPrice: "193.00"
tourId: "20586"