你可以用
json.loads
(或
json.load
如果您直接从文件中读取,请将文本(json格式)转换为python对象(嵌套列表/目录),然后使用列表理解:
import json
text = '''[
{
"title": "5280 Cafe At Rallysport",
"streetAddress": "2727 29th St.",
"addressLocality": "Boulder",
"addressRegion": "CO",
"postalCode": "80301",
"phoneNumber": "720-526-1013",
"vendorCuisine": "Breakfast"
},
{
"title": "Ali Baba Grill Boulder",
"streetAddress": "3054 28th St",
"addressLocality": "Boulder",
"addressRegion": "CO",
"postalCode": "80304",
"phoneNumber": "303-440-1393",
"vendorCuisine": "Mediterranean"
}]'''
output = [d['title'] for d in json.loads(text)]
print(output) # ['5280 Cafe At Rallysport', 'Ali Baba Grill Boulder']
如果json文本存储在,
restaurants.json
,然后:
with open('restaurants.json', 'r') as f:
output = [d['title'] for d in json.load(f)]