/* 时间: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, " ") ########## 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, " ") ########## # 数据转换 : 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, " ") dict_to_str = str(dict) # 数据转换 : dict - str print(type(dict_to_str), dict_to_str, " ") str_to_dict = eval(dict_to_str) # 数据转换 : str - dict print(type(str_to_dict), str_to_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': 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":"[xe5xa4xa7xe5x90x89+xe8xb4xa2xe8xbfx90+xe5xaex98xe8xbfx90]xe5xa4xa9xe8xb5x8bxe5x90x89xe8xbfx90xefxbcx8cxe5xbexb7xe6x9cx9bxe5x85xbcxe5xa4x87xefxbcx8cxe7xbbxa7xe7xbbxadxe5x8axaaxe5x8ax9bxefxbcx8cxe5x89x8dxe9x80x94xe6x97xa0xe9x99x90","analysis":"xe5xbexb7xe6x9cx9bxe9xabx98xe5xa4xa7xe5x90x8dxe8xaax89xe6x8cxafxefxbcx8cxe6x89x8dxe8xb0x8bxe5x81xa5xe5x85xa8xe8xb4xa2xe6xbax90xe8xbfx9bxefxbcx8cxe5xafx8cxe8xb4xb5xe8x8dxa3xe5x8dx8exe7xa6x8fxe7xa6x84xe8x87xb3xefxbcx8cxe5x89x8dxe9x80x94xe6xb4x8bxe6xb4x8bxe5xbex97xe6x84x8fxe7x9cx9fxe3x80x82"}}}' {"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