[英]How to extract nested json object values Python
有人可以帮助提取嵌套的 json 值。 我有一个 json 结构。 我只想从嵌套的 json 中获取值,不包括密钥。 我们是否有任何预定义的函数可以使用
{
'catalog': [
{
'catalog_num': 120,
'type': {
'screen_inch': 18.0,
'screen_width': 240.0,
'screen_height': 78.0
}
},
{
'catalog_num': 9992,
'type': {
'screen_inch': 18.0,
'screen_width': 36.0,
'screen_height': 78.0
}
},
{
'catalog_num': 100,
'type': {
'screen_inch': 24.0,
'screen_width': 576.0,
'screen_height': 78.0
}
}
]
}
我想要这种格式的 output:-
{
'catalog': [
{
'catalog_num': 120,
'screen_inch': 18.0,
'screen_width': 240.0,
'screen_height': 78.0
},
{
'catalog_num': 9992,
'screen_inch': 18.0,
'screen_width': 36.0,
'screen_height': 78.0
},
{
'catalog_num': 100,
'screen_inch': 24.0,
'screen_width': 576.0,
'screen_height': 78.0
}
]
}
有没有办法只从嵌套的 json 中提取值。 我希望从我的 output 中删除“类型”,并为现有的 object 添加价值
我假设你得到了一个字典/列表结构而不是一个 json 字符串(如果是后者,使用json.loads )。 那么你的问题基本上是
我如何转换
d = { key_1: value_1, key_2: {key_3: value_3} }
进入
d = { key_1: value_1, key_3: value_3 }
?
一种方法是
d.update(d.pop(key_2))
有关dict.update
和dict.pop
的解释,请参阅dict的文档 :)
现在对于您的用例,您只需对'category'
列表中的每个字典重复该操作,可能使用for
循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.