我试图在json文件中导航,但无法正确解析“头条新闻”节点。
以下是我的JSON文件:
{
"resultsPage":{
"results":{
"calendarEntry":[
{
"event":{
"id":38862824,
"artistName":"Raphael",
},
"performance":[
{
"id":73632729,
"headlinerName":"Top-Secret",
}
}
],
"venue":{
"id":4285819,
"displayName":"Sacré"
}
}
}
}
以下是我想做的事:
for item in data ["resultsPage"]["results"]["calendarEntry"]:
artistname = item["event"]["artistName"]
headliner = item["performance"]["headlinerName"]
我不明白为什么它为'artistName‘工作,但它不工作’头条名称‘。谢谢你的帮助和解释。
发布于 2019-08-07 20:40:47
注意您的performance
密钥:
"performance":[
{
"id":73632729,
"headlinerName":"Top-Secret",
}
}
],
你贴的那个json是畸形的。假设结构类似于:
"performance":[
{
"id":73632729,
"headlinerName":"Top-Secret",
}
],
你可以:
for i in item:
i["headlinerName"]
或者就像@UltraInstinct建议的那样:
项目“性能”“headlinerName”
发布于 2019-08-07 20:41:16
这里有几个问题。首先,您的JSON格式不正确。你的方括号不匹配。也许你是说这样的事?我假设"calendarEntry“在这里是一个列表,其他的东西都是一个对象。通常,列表是复数的,即"calendarEntries“。
{
"resultsPage": {
"results": {
"calendarEntries": [
{
"event": {
"id": 38862824,
"artistName": "Raphael"
},
"performance": {
"id": 73632729,
"headlinerName": "Top-Secret"
},
"venue": {
"id": 4285819,
"displayName": "Sacré"
}
}
]
}
}
}
https://stackoverflow.com/questions/57401962
复制相似问题