[英]python unable to parse JSON Data
I am unable to parse the JSON data using python. 我无法使用python解析JSON数据。
A webpage url is returning JSON Data 网页网址返回JSON数据
import requests
import json
BASE_URL = "https://www.codechef.com/api/ratings/all"
data = {'page': page, 'sortBy':'global_rank', 'order':'asc', 'itemsPerPage':'40' }
r = requests.get(BASE_URL, data = data)
receivedData = (r.text)
print ((receivedData))
when I printed this, I got large text and when I validated using https://jsonlint.com/ it showed VALID JSON 当我打印此文件时,我得到了大文本,当我使用https://jsonlint.com/进行验证时,它显示了VALID JSON
Later I used 后来我用
import requests
import json
BASE_URL = "https://www.codechef.com/api/ratings/all"
data = {'page': page, 'sortBy':'global_rank', 'order':'asc', 'itemsPerPage':'40' }
r = requests.get(BASE_URL, data = data)
receivedData = (r.text)
print (json.loads(receivedData))
When I validated the large printed text using https://jsonlint.com/ it showed INVALID JSON 当我使用https://jsonlint.com/验证较大的打印文本时,它显示了无效的JSON
Even if I don't print and directly use the data. 即使我不打印并直接使用数据。 It is working properly.
它工作正常。 So I am sure even internally it is not loading correctly.
因此,我确定即使在内部也无法正确加载。
is python unable to parse the text to JSON properly? 是python无法将文本正确解析为JSON?
in short, json.loads
converts from a Json (thing, objcet, array, whatever) into a Python object - in this case, a Json Dictionary. 简而言之,
json.loads
从Json(事物,objcet,数组等)转换为Python对象-在这种情况下为Json字典。 When you print that, it will print as a itterative and therefore print with single quotes.. 当您打印时,它将打印为命令式,因此使用单引号引起来。
Effectively your code can be expanded: 有效地可以扩展您的代码:
some_dictionary = json.loads(a_string_which_is_a_json_object)
print(some_dictionary)
to make sure that you're printing json-safe, you would need to re-encode with json.dumps
为了确保您要打印json-safe,您需要使用
json.dumps
重新编码
When you use python's json.loads(text)
it returns a python dictionary. 当您使用python的
json.loads(text)
它将返回一个python字典。 When you print that dictionary out it is not in json format. 当您打印该词典时,它不是json格式。
If you want a json output you should use json.dumps(json_object)
. 如果要json输出,则应使用
json.dumps(json_object)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.