1.响应转化为json字符串
import requests import json re=requests.get('https://github.com/timeline.json') print(re.text) js=re.json() #响应转化为json字符串 print(js)
# key_value=js['documentation_url'] #取键对应的值
# print(key_value)
响应:
{"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"} {'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.', 'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events'}
2.序列化
import requests import json url = 'https://api.github.com/some/endpoint' payload={'some':'data','list':'[1,True]'} # print(type(payload)) re=requests.post(url,data=json.dumps(payload)) #反序列化 # re=requests.post(url,data=payload) print(re.text) print(re.json())
响应;
{"message":"Not Found","documentation_url":"https://developer.github.com/v3"} {'message': 'Not Found', 'documentation_url': 'https://developer.github.com/v3'}