016_v2 python接口 类型转换
/* 时间:2020/10/21 功能:类型转换 目录: 一: dict <-> json 二: dict <-> str 三: return 1 三种 2 json 四: 传参json */
一: dict <-> json
#coding:utf-8 import json dict = { "a": None, "b": False, "c": 123, "d": "string", "tuple": (123, "str"), "subject_list" : ["English", 123, "Math"], "dict" : { "English" : 88, "Chinese" : 92, "Math" : 100 } } print(type(dict), dict, "\n") ########## data_json = json.dumps(dict) # 数据转换 : dict - json print(type(data_json), data_json) data_dict = json.loads(data_json) # 数据转换 : json - dict print(type(data_dict), data_dict, "\n\n") ########## # 数据转换 : json - dict str_json = '{"a": null, "b": false, "c": 123, "d": "string", "tuple": [123, "str"], "subject_list": ["English", 123, "Math"], "dict": {"English": 88, "Chinese": 92, "Math": 100}}' print(type(str_json), str_json) data_dict = json.loads(str_json) print(type(data_dict), data_dict)
<class 'dict'> {'a': None, 'b': False, 'c': 123, 'd': 'string', 'tuple': (123, 'str'), 'subject_list': ['English', 123, 'Math'], 'dict': {'English': 88, 'Chinese': 92, 'Math': 100}} <class 'str'> {"a": null, "b": false, "c": 123, "d": "string", "tuple": [123, "str"], "subject_list": ["English", 123, "Math"], "dict": {"English": 88, "Chinese": 92, "Math": 100}} <class 'dict'> {'a': None, 'b': False, 'c': 123, 'd': 'string', 'tuple': [123, 'str'], 'subject_list': ['English', 123, 'Math'], 'dict': {'English': 88, 'Chinese': 92, 'Math': 100}} <class 'str'> {"a": null, "b": false, "c": 123, "d": "string", "tuple": [123, "str"], "subject_list": ["English", 123, "Math"], "dict": {"English": 88, "Chinese": 92, "Math": 100}} <class 'dict'> {'a': None, 'b': False, 'c': 123, 'd': 'string', 'tuple': [123, 'str'], 'subject_list': ['English', 123, 'Math'], 'dict': {'English': 88, 'Chinese': 92, 'Math': 100}}
二: dict <-> str
#coding:utf-8 dict = { "a": None, "b": False, "c": 123, "d": "string", "tuple": (123, "str"), "subject_list" : ["English", 123, "Math"], "dict" : { "English" : 88, "Chinese" : 92, "Math" : 100 } } print(type(dict), dict, "\n") dict_to_str = str(dict) # 数据转换 : dict - str print(type(dict_to_str), dict_to_str, "\n") str_to_dict = eval(dict_to_str) # 数据转换 : str - dict print(type(str_to_dict), str_to_dict, "\n")
<class 'dict'> {'a': None, 'b': False, 'c': 123, 'd': 'string', 'tuple': (123, 'str'), 'subject_list': ['English', 123, 'Math'], 'dict': {'English': 88, 'Chinese': 92, 'Math': 100}} <class 'str'> {'a': None, 'b': False, 'c': 123, 'd': 'string', 'tuple': (123, 'str'), 'subject_list': ['English', 123, 'Math'], 'dict': {'English': 88, 'Chinese': 92, 'Math': 100}} <class 'dict'> {'a': None, 'b': False, 'c': 123, 'd': 'string', 'tuple': (123, 'str'), 'subject_list': ['English', 123, 'Math'], 'dict': {'English': 88, 'Chinese': 92, 'Math': 100}}
三: return
1 三种
#coding:utf-8 import requests url = "http://japi.juhe.cn/qqevaluate/qq?key=980bf619855953f6ebef9abe90d52712&qq=384056521" r = requests.get(url) print(r.content) # 字节输出 print(r.text) # str输出 print(r.json()) # 字典格式
b'{"error_code":0,"reason":"success","result":{"data":{"conclusion":"[\xe5\xa4\xa7\xe5\x90\x89+\xe8\xb4\xa2\xe8\xbf\x90+\xe5\xae\x98\xe8\xbf\x90]\xe5\xa4\xa9\xe8\xb5\x8b\xe5\x90\x89\xe8\xbf\x90\xef\xbc\x8c\xe5\xbe\xb7\xe6\x9c\x9b\xe5\x85\xbc\xe5\xa4\x87\xef\xbc\x8c\xe7\xbb\xa7\xe7\xbb\xad\xe5\x8a\xaa\xe5\x8a\x9b\xef\xbc\x8c\xe5\x89\x8d\xe9\x80\x94\xe6\x97\xa0\xe9\x99\x90","analysis":"\xe5\xbe\xb7\xe6\x9c\x9b\xe9\xab\x98\xe5\xa4\xa7\xe5\x90\x8d\xe8\xaa\x89\xe6\x8c\xaf\xef\xbc\x8c\xe6\x89\x8d\xe8\xb0\x8b\xe5\x81\xa5\xe5\x85\xa8\xe8\xb4\xa2\xe6\xba\x90\xe8\xbf\x9b\xef\xbc\x8c\xe5\xaf\x8c\xe8\xb4\xb5\xe8\x8d\xa3\xe5\x8d\x8e\xe7\xa6\x8f\xe7\xa6\x84\xe8\x87\xb3\xef\xbc\x8c\xe5\x89\x8d\xe9\x80\x94\xe6\xb4\x8b\xe6\xb4\x8b\xe5\xbe\x97\xe6\x84\x8f\xe7\x9c\x9f\xe3\x80\x82"}}}' {"error_code":0,"reason":"success","result":{"data":{"conclusion":"[大吉+财运+官运]天赋吉运,德望兼备,继续努力,前途无限","analysis":"德望高大名誉振,才谋健全财源进,富贵荣华福禄至,前途洋洋得意真。"}}} {'error_code': 0, 'reason': 'success', 'result': {'data': {'conclusion': '[大吉+财运+官运]天赋吉运,德望兼备,继续努力,前途无限', 'analysis': '德望高大名誉振,才谋健全财源进,富贵荣华福禄至,前途洋洋得意真。'}}}
2 json
#coding:utf-8 import requests url = "http://japi.juhe.cn/qqevaluate/qq?key=980bf619855953f6ebef9abe90d52712&qq=384056521" r = requests.get(url) data_dict = r.json() print(type(data_dict), data_dict) print(data_dict["result"]["data"]["analysis"])
<class 'dict'> {'error_code': 0, 'reason': 'success', 'result': {'data': {'conclusion': '[大吉+财运+官运]天赋吉运,德望兼备,继续努力,前途无限', 'analysis': '德望高大名誉振,才谋健全财源进,富贵荣华福禄至,前途洋洋得意真。'}}} 德望高大名誉振,才谋健全财源进,富贵荣华福禄至,前途洋洋得意真。
四: 传参json
# 传参数据格式 - 要求json # 方法一 import requests dict = { "a": None, "b": False, "c": 123, "d": "string", "tuple": (123, "str"), "subject_list" : ["English", 123, "Math"], "dict" : { "English" : 88, "Chinese" : 92, "Math" : 100 } } url = "http://bin.org/post" r = requests.post(url, json=dict) # 自动转换 : dict - json # 传参数据格式 - 要求json # 方法二 import requests import json dict = { "a": None, "b": False, "c": 123, "d": "string", "tuple": (123, "str"), "subject_list" : ["English", 123, "Math"], "dict" : { "English" : 88, "Chinese" : 92, "Math" : 100 } } url = "http://bin.org/post" r = requests.post(url, data=json.dumps(dict)) # 自动转换 : dict - json