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

前端 未结 2 813
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 14:11

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.

Wha

相关标签:
2条回答
  • 2020-11-22 14:18

    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.

    0 讨论(0)
  • 2020-11-22 14:38

    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().

    0 讨论(0)
提交回复
热议问题