【发布时间】:2016-09-23 18:07:23
【问题描述】:
我对 JSON 很陌生。我的代码包括从需要 API 密钥的网站中提取数据。提取了信息。我正在尝试通过这种格式获取以儿子编码的信息(这是一个示例):
[{"number":31705,"name":"31705 - CHAMPEAUX (BAGNOLET)","address":"RUE DES CHAMPEAUX (PRES DE LA GARE ROUTIERE) - 93170 BAGNOLET","latitude":48.8645278209514,"longitude":2.416170724425901},{"number":10042,"name":"10042 - POISSONNIÈRE - ENGHIEN","address":"52 RUE D'ENGHIEN / ANGLE RUE DU FAUBOURG POISSONIERE - 75010 PARIS","latitude":48.87242006305313,"longitude":2.348395236282807}]
如何访问 json 代码中的不同数据?这是我想出的代码:
import requests
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{station_number}?contract={contract_name}&api_key HTTP/1.1')
我相信我的请求已经在网站发送到我的计算机的“响应”“文件夹”中形成了响应:
print(reponse.headers)
print(reponse(2,/'latitude')
我正在尝试访问 json 代码的每个元素中的纬度信息 - 2 表示列表的第二个元素,纬度表示我试图在 json 列表的元素中访问的值的名称。但我无法做到。我得到的错误是语法错误。
我该如何解决?我想访问对象“响应”的每个成员的每个字符串的所有值。
更新 n°1:
我的新代码是:
import json
import requests
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&apiKey={0617697a9795f803697de4b9abf9759d5406b3a0} HTTP/1.1')
data = json.loads(response.content)
print(data)
但是我得到了错误:
Traceback (most recent call last):
File "/Users/someone/Desktop/TIPE 2016:17/Programme TIPE 2016:2017.py", line 27, in <module>
data = json.loads(response.content)
File "/Users/someone/miniconda3/lib/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
更新 n°2:
我的新代码是:
import json
import requests
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&apiKey={0617697a9795f803697de4b9abf9759d5406b3a0} HTTP/1.1')
data = response.json()
latitude = data[2]['latitude']
但是我得到了错误:
Traceback (most recent call last):
File "/Users/someone/Desktop/TIPE 2016:17/Programme TIPE 2016:2017.py", line 30, in <module>
latitude = data[2]['latitude']
KeyError: 2
是否意味着响应为空?
更新 n°3:
reponse.content
答案如下:
b'{ "error" : "Unauthorized" }'
有什么问题?
更新 n°4:
我的新代码是:
reponse=requests.get('https://api.jcdecaux.com/vls/v1/stations/{31705}?contract={Paris}&apiKey={0617697a9795f803697de4b9abf9759d5406b3a0} HTTP/1.1')
data = json.loads(response.content.decode('utf-8'))
print(reponse.headers)
print(reponse.content)
结果是:
{'Content-Length': '48', 'Content-Encoding': 'gzip', 'Server': 'Apache-Coyote/1.1', 'Date': 'Fri, 23 Sep 2016 19:39:25 GMT', 'Connection': 'close', 'Content-Type': 'application/json'}
b'{ "error" : "Unauthorized" }'
所以我的请求的答案不是空的,但我没有访问它的授权。我该如何解决这个问题?
最终更新:
新的工作代码是:
import json
import requests
r = requests.get('https://api.jcdecaux.com/vls/v1/stations/31705?contract=Paris&apiKey=0617697a9795f803697de4b9abf9759d5406b3a0')
response_json = r.json()
print (response_json['name'])
结果是:
31705 - CHAMPEAUX (BAGNOLET)
【问题讨论】:
-
你想用
print(reponse(2,/'latitude')实现什么,因为它缺少右括号)和/'latitude'参数很奇怪。 -
我正在尝试访问 json 代码的每个元素中的纬度信息 - 2 表示列表的第二个元素,纬度表示我试图在元素中访问的值的名称json 列表。但我做不到。
-
你能检查一下
response.content的值是多少吗? -
哦,上面写着:b'{ "error" : "Unauthorized" }' 这是什么意思?
-
您提出了未经授权的请求。您的身份验证令牌设置不正确。