問題描述
使用python的json模塊解析json請求 (Parse a json request using json module of python)
我想解析 json 請求(GET https ://api.twitter.com/1.1/favorites/list.json?count=2&screen_name=episod) 使用 python。請告訴我該怎麼做。
參考解法
方法 1:
You can use requests.get()
to make a GET request to your URL and get the JSON string:
from requests import get
URL = "https://api.twitter.com/1.1/favorites/list.json?count=2&screen_name=episod"
res = get(URL).text
print(res)
# {"errors":[{"code":215,"message":"Bad Authentication data."}]}
print(type(res))
# <class 'str'>
Then you can use json.loads()
to convert the JSON string into a python dictionary:
from json import loads
json_dict = loads(res)
print(json_dict)
# {'errors': [{'code': 215, 'message': 'Bad Authentication data.'}]}
print(type(json_dict))
# <class 'dict'>
Which you can iterate and parse the information you want.
(by nandini banerjee、RoadRunner)