我在过滤多个json数据时遇到了一些困难,我需要知道每个数据的type
,如果类型对应于一个水果,那么打印元素的fields
键,请参阅python示例注释以获得更好的解释。在
以下是JSON的外观:
#json.items()
{
'type': 'apple',
'fields': {
'protein': '18g',
'glucide': '3%',
}
},
{
'type': 'banana',
'fields': {
'protein': '22g',
'glucide': '8%',
}
},
我试着这么做:
^{pr2}$我有办法做到吗?在
根据您对JSON的表示,它实际上是一个列表,而不是字典。所以为了遍历它,你可以尝试这样的方法:
你所拥有的似乎是一系列名言。在
然后检查键}是否存在于字典中,然后再检查它们的值,如下所示:
type
和{相关问题 更多 >
编程相关推荐