What's the best way to parse a JSON response f

2020-01-22 12:51发布

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?

2条回答
Fickle 薄情
2楼-- · 2020-01-22 13:23

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.

查看更多
闹够了就滚
3楼-- · 2020-01-22 13:23

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.

查看更多
登录 后发表回答