【发布时间】:2021-03-15 15:57:38
【问题描述】:
我有一个 JSON 对象,如下所示。
[
{
"metadata": {
"Name": "Mike",
"Age": 28,
"DOB": "05/19/1992",
"Profile" : {
"type" : "standard",
"payment" : "credit_card"
},
"Id" : "xxxyyxx"
},
"other" : False,
"statistics": {
"clicks": 32,
"comments": "some text here"
}
},
{
"metadata": {
"Name": "Andy",
"Age": 24,
"DOB": "10/01/1989",
"Profile" : {
"type" : "standard",
"payment" : "credit_card"
},
"Id" : "xxyyyxx"
},
"other" : False,
"statistics": {
"clicks": 17,
"comments": "some text here"
}
},
]
我想删除此 JSON 对象中的元素,以便将其展平,如下所示,同时删除不必要的项目。我希望它如下所示。
[
{
"Id" = "xxxyyxx"
"clicks": 32
"comments": "some text here"
},
{
"Id" = "xxyyyxx"
"clicks": 17
"comments": "some text here"
}
]
我尝试尝试使用 pop 删除对象,但我收到“RuntimeError: dictionary changed size during iteration”。对我来说,在 Python 中进行此操作的最佳方式是什么?
【问题讨论】:
-
那是无效的 JSON。
-
@VishalSingh 为什么无效?
-
查看这里为什么无效jsonformatter.curiousconcept.com
-
我修复了 JSON 格式。我对如何解析这样的结构更感兴趣。
-
@Sue_ka 我已经添加了完整工作演示的答案,见下文:stackoverflow.com/a/66641714/1138192
标签: python json python-2.7 nested-lists