我试图在python的json输出中获取与name相关的值。我不知道怎么做

时间:2015-06-30 06:35:21

标签: python json

import urllib.request
import json
import io
from urllib.request import urlopen
u=io.TextIOWrapper(urlopen('https://api.locu.com/v1_0/venue/search/?locality=Atlanta&postal_code=30301&category=restaurant&api_key=xxxxx'),encoding='latin1')
text=u.read()
print (text)

output is as follows:
{"meta": {"limit": 25, "cache-expiry": 3600}, "objects": [{"name": "Mali Restaurant", "locality": "Atlanta", "street_address": "961 Amsterdam Ave.", "cuisines": [], "region": "GA", "long": -84.355387, "phone": "(404) 874-1411", "postal_code": "30301", "categories": ["other", "restaurant"], "has_menu": true, "country": "United States", "lat": 33.787547, "id": "c0f62e6ab2d8ed4a169f", "website_url": ", "resource_uri": "/v1_0/venue/c0f62e6ab2d8ed4a169f/"}, {"name": "Coca-Cola CCP Cafe", "locality": "Atlanta", "street_address": "One Coca-Cola Plaz", "cuisines": [], "region": "GA", "long": -84.3976825046326, "phone": null, "postal_code": "30301", "categories": ["other", "restaurant"], "has_menu": false, "country": "United States", "lat": 33.7713581924234, "id": "c4970097bd8b63fad908", "website_url": null, "resource_uri": "/v1_0/venue/c4970097bd8b63fad908/"}, {"name": "Boner's", "locality": "Atlanta", "street_address": "634 Fraser St.", "cuisines": [], "region": "GA", "long": -84.386258, "phone": "(404) 659-9000", "postal_code": "30301", "categories": ["restaurant"], "has_menu": true, "country": "United States", "lat": 33.737016, "id": "f5bf05f7d9bbc609a1d1", "website_url": ", "resource_uri": "/v1_0/venue/f5bf05f7d9bbc609a1d1/"}]}

我想获得" name"的值。我该怎么办?

1 个答案:

答案 0 :(得分:0)

该文本中有错误,这是无效的JSON:

"website_url": ", "resource_uri":

但如果它是有效的JSON,你可以这样做......

j = json.loads(text)
for node in j['objects']:
  if 'name' in node:
    print (node['name'])