从请求库中解析JSON响应的最佳方法是什么?

发布于 2025-02-04 19:31:41 字数 241 浏览 1 评论 0 原文

我正在使用python request module 将其发送到服务器,为此,我在JSON中得到了回应。 JSON响应基本上只是列表。

胁迫对本机Python对象的响应的最佳方法是什么,以便我可以使用 pprint 迭代或打印出来?

I'm using the python requests module to send a RESTful GET to a server, for which I get a response in JSON. The JSON response is basically just a list of lists.

What's the best way to coerce the response to a native Python object so I can either iterate or print it out using pprint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千年*琉璃梦 2025-02-11 19:31:41

由于您正在使用请求,因此应使用响应的 json 方法。

import requests

response = requests.get(...)
data = response.json()

Since you're using requests, you should use the response's json method.

import requests

response = requests.get(...)
data = response.json()

It autodetects which decoder to use.

携君以终年 2025-02-11 19:31:41

您可以使用 json.loads

import json
import requests

response = requests.get(...)
json_data = json.loads(response.text)

这将给定的字符串转换为字典,使您可以在代码中轻松访问JSON数据。

或者,您可以使用 @martijn的有用建议,而较高的投票答案, wendesp.json()

You can use json.loads:

import json
import requests

response = requests.get(...)
json_data = json.loads(response.text)

This converts a given string into a dictionary which allows you to access your JSON data easily within your code.

Or you can use @Martijn's helpful suggestion, and the higher voted answer, response.json().

梦与时光遇 2025-02-11 19:31:41

您可以直接将JSON响应用作字典:

import requests

res = requests.get('https://reqres.in/api/users?page=2')
print(f'Total users: {res.json().get("total")}')

或者您可以将JSON内容作为字典将其保存为字典:

json_res = res.json()

从此 JSON_RES 字典变量,您可以提取您选择的任何值

json_res.get('total')
json_res["total"]

actentions 因为这是一本词典,所以您应该关注关键拼写,并且情况下, ie“总”与'total''不同

You can use the json response as dictionary directly:

import requests

res = requests.get('https://reqres.in/api/users?page=2')
print(f'Total users: {res.json().get("total")}')

or you can hold the json content as dictionary:

json_res = res.json()

and from this json_res dictionary variable, you can extract any value of your choice

json_res.get('total')
json_res["total"]

Attentions Because this is a dictionary, you should keep your eye on the key spelling and the case, i.e. 'total' is not the same as 'Total'

也只是曾经 2025-02-11 19:31:41

从请求库中解析JSON响应的最佳方法是什么?

顶部答案显示似乎是将JSON响应解析为Python对象的两种不同的方法,但它们基本上是相同的。

response.json()在两个地方有所不同:

因此,在99.9999%的情况下, wendesp.json() and json.loads(wendesp.text)将产生相同的字典。如果源JSON具有一些怪异的编码,则可能会有所不同。

What's the best way to parse a JSON response from the requests library?

The top answers show seemingly two different ways to parse a json response into a Python object but they are essentially the same.

response.json() differs in two places:

  • it uses simplejson (which is the externally maintained development version of the json library included with Python) if it's installed in your environment, but uses the built-in json if not (source). I think there used to be a performance difference between json and simplejson in the past (when Python 2 was still widely used) but there's almost no difference between the libraries anymore.
  • if the response doesn't have an encoding (response.encoding is None), then it tries to guess it and try to decode using the guessed encoding (source).

So in 99.9999% of cases, response.json() and json.loads(response.text) will produce the same dictionary. It may differ if the source json has some weird encoding.

如梦亦如幻 2025-02-11 19:31:41

如果不json

# pip install requests
import requests

r = requests.get(url)
try:
    data = r.json()
except requests.JSONDecodeError:
    content_type = r.headers.get('Content-Type')
    print(content_type)
    # r.encoding='utf-8'
    # data = r.text

in case of not json

# pip install requests
import requests

r = requests.get(url)
try:
    data = r.json()
except requests.JSONDecodeError:
    content_type = r.headers.get('Content-Type')
    print(content_type)
    # r.encoding='utf-8'
    # data = r.text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文