我正在尝试返回特定键的字段和值,但出现错误 这是示例json格式:
"results": [
{
"time": "00:00",
"app_name": "dcg",
"avg": "7717"
},
{
"time": "00:00",
"app_name": "pds",
"avg": "75.40223463687151"
},
{
"time": "00:00",
"app_name": "rdts",
"avg": "1481.5555555555557"
},
{
"time": "00:00",
"app_name": "slbl",
"avg": "786"
},
{
"time": "01:00",
"app_name": "pds",
"avg": "36.4765625"
}
这是我的代码
import json
json_data=open("some.json")
jdata = json.load(json_data)
for k, v in jdata.results.items():
for k1, v1 in v.items():
print(k1)
print(v1)
请注意,结果是键和时间字段,其中包含多个具有相同值的条目。
答案 0 :(得分:0)
除了修复损坏的缩进,您还可以尝试以下方法。 results
是字典的键,其值可以作为jdata["results"]
访问。
for v in jdata["results"]:
for k1, v1 in v.items():
print(k1)
print(v1)
编辑:要将它们存储在列表中,您可以执行
result = []
for v in jdata["results"]:
for k1, v1 in v.items():
result.append({"k1":v1})
[{'k1': '00:00'},
{'k1': 'dcg'},
{'k1': '7717'},
{'k1': '00:00'},
{'k1': 'pds'},
{'k1': '75.40223463687151'},
{'k1': '00:00'},
{'k1': 'rdts'},
{'k1': '1481.5555555555557'},
{'k1': '00:00'},
{'k1': 'slbl'},
{'k1': '786'},
{'k1': '01:00'},
{'k1': 'pds'},
{'k1': '36.4765625'}]
答案 1 :(得分:0)
我不得不把你的JSON在含组括号{}否则JSON处理器不能解析它的....
即...
{ 'result' : [...] }