在JSON中查找元素的最快速方法(Python)
这个问题在这里已经有了答案:
这是一个简单的方法来做到这一点:
def function(json_object, name):
for dict in json_object:
if dict['name'] == name:
return dict['price']
如果你确定没有重复的名字,那么更有效的(和pythonic的方法是使用列表解析:
def function(json_object, name):
return [obj for obj in json_object if obj['name']==name][0]['price']
from json import loads
json = """[
{
"name":"Apple",
"price":2,
"have":0,
"max":36
},
{
"name":"Pineapple",
"price":5,
"have":6,
"max":17
}
]"""
parsedJson = loads (json)
def jsonname (name):
for entry in parsedJson:
if name == entry ['name']:
return entry ['price']
链接地址: http://www.djcxy.com/p/38041.html