【发布时间】:2015-03-25 22:02:34
【问题描述】:
我已经加载了一个 JSON 文件,但我无法解析它以更新或插入值。
JSON 结构和这个类似:
{
"nodes": [
{
"id": "node1",
"x": 21.0,
"y": 8.0
},
{
"id": "node5",
"x": 3.0,
"y": 5.0
}
]
}
虽然我检索节点的 python 代码与此类似:
jsonData = defaultdict(list)
with open('data.json', 'r') as f:
jsonData = json.load(f)
print jsonData['nodes']['id'] == 'node5'
我得到的错误是“TypeError: list indices must be integers, not str”。
如何检索节点以及如何更新它?
【问题讨论】:
-
顺便说一句,您拥有的 JSON 已损坏,倒数第三行在
}之后有一个,,它不应该存在。 -
谢谢,是我的错,幸好只是复制粘贴不好,原来的JSON没问题。
标签: python json parsing dictionary