The Fastest method to find element in JSON (Python)
This question already has an answer here:
Here's an easy way to do it:
def function(json_object, name):
for dict in json_object:
if dict['name'] == name:
return dict['price']
If you are sure that there are no duplicate names, an even more effective (and pythonic way to do it is to use list comprehensions:
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/38042.html