简体   繁体   English

无法使用python解析json响应

[英]Unable to parse json response with python

So I ran into an issue while importing API data into my code. 因此,在将API数据导入我的代码时遇到了一个问题。 Any help is much appreciated. 任何帮助深表感谢。

from urllib2 import Request, urlopen, URLError
import json, requests

data = requests.get('https://masari.superpools.net/api/live_stats?update=1522693430318').json()
data_parsed = json.loads(open(data,"r").read())
print data_parsed

I'm still quite new to python, and I ran into this error: 我对python还是很陌生,但遇到了这个错误:

>C:\Users\bot2>python C:\Users\bot2\Desktop\Python_Code\helloworld.py
Traceback (most recent call last):
  File "C:\Users\bot2\Desktop\Python_Code\helloworld.py", line 5, in <module>
    data_parsed = json.loads(open(data,"r").read())
TypeError: coercing to Unicode: need string or buffer, dict found

data is already received as a json object (which is a dict in this case). data已作为json对象接收(在这种情况下为dict )。 Just do the following: 只需执行以下操作:

data = requests.get('https://masari.superpools.net/api/live_stats?update=1522693430318').json()
print data

Use data['network'] for example to access nested dictionaries. 例如,使用data['network']访问嵌套字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM