我试图在python的json输出中获取与name关联的值。我不知道怎么做
我想得到“name”的值。我该怎么办?该文本中有一个错误,这是无效的JSON:我试图在python的json输出中获取与name关联的值。我不知道怎么做,python,json,Python,Json,我想得到“name”的值。我该怎么办?该文本中有一个错误,这是无效的JSON: “网站url”:“资源uri”: 但如果它是有效的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&posta
“网站url”:“资源uri”:
但如果它是有效的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/"}]}
太棒了。它实际上要求我在我的问题中不要使用两个以上的URL。所以我不得不删除它们。它工作得很好。非常感谢。
j = json.loads(text)
for node in j['objects']:
if 'name' in node:
print (node['name'])