赞
踩
详见博文:json.dumps()、json.dump()、json.loads()和json.dumps()的区别_IT之一小佬的博客-CSDN博客
代码如下:
- import json
-
- dict1 = {'A': 'a', 'B': 'b', 'C': 'c'}
- print(type(dict1))
- print(dict1)
-
- # python编码为json类型,json.dumps()
- en_json = json.dumps(dict1)
- print(type(en_json))
- print(en_json)
-
- # json解码为python类型, json.loads()
- de_json = json.loads(en_json)
- print(type(de_json))
- print(de_json)
-
- # eval()解析json
- dict2 = {'X': 'x', 'Y': 'y', 'Z': 'z'}
- en_json = json.dumps(dict2)
- de_json = eval(en_json)
- print(type(de_json))
- print(de_json)

运行结果:
- <class 'dict'>
- {'A': 'a', 'B': 'b', 'C': 'c'}
- <class 'str'>
- {"A": "a", "B": "b", "C": "c"}
- <class 'dict'>
- {'A': 'a', 'B': 'b', 'C': 'c'}
- <class 'dict'>
- {'X': 'x', 'Y': 'y', 'Z': 'z'}
有2种方法可以处理request返回的数据:
1.对request发起请求返回的响应对象进行.json()操作,.json操作返回的是一个字典类型(如果返回的结果为空时则会报错),如下:
2.对request请求得到的响应对象中的text数据进行json.loads(text)操作,操作后返回的是字典类型(建议使用这种方法,使用前判断text是否为空,为空也会报错),如下:
示例代码:
- import json
-
- list1 = [{'A': [1, 2, 3, 4, 5, 6], 'B': [3, 4, 5, 6, 7]},
- {'C': '123', 'D': '234'}]
-
- # 将数据写入文件
- file = open('json_demo.json', 'w')
- for i in list1:
- json_i = json.dumps(i)
- file.write(json_i + '\n')
- file.close()
-
- # 从文件中读取数据
- get_data = []
- with open('json_demo.json', 'r') as f:
- # 读取数据并分割,最后一个为空,所以去除
- new_list = f.read().split('\n')[:-1]
- for x in new_list:
- json_x = json.loads(x)
- print(type(json_x))
- get_data.append(json_x)
- f.close()
-
- print(f'原始数据为:{list1}')
- print(f'结果数据为:{get_data}')

运行结果:
- <class 'dict'>
- <class 'dict'>
- 原始数据为:[{'A': [1, 2, 3, 4, 5, 6], 'B': [3, 4, 5, 6, 7]}, {'C': '123', 'D': '234'}]
- 结果数据为:[{'A': [1, 2, 3, 4, 5, 6], 'B': [3, 4, 5, 6, 7]}, {'C': '123', 'D': '234'}]
使用json在线转义工具:JSON在线 | JSON解析格式化—SO JSON在线工具
使用代码进行转义,示例代码如下:
- import json
-
-
- dic = {'name': 'dgw', 'sex': '男', 'age': 26, 'grade': [{'语文': 96}, {'数学': 99}]}
- print(dic)
-
- # json数据是双引号
- json_dic = json.dumps(dic, ensure_ascii=False)
- print(json_dic)
-
- # 对json_dic数据进行转义
- json_dic_escape = json.dumps(json_dic, ensure_ascii=False)
- print(json_dic_escape)
-
- # 对dic数据中某个字段单独转义
- dic['grade'] = json.dumps(dic['grade'], ensure_ascii=False)
- json_dic_escape2 = json.dumps(json.dumps(dic, ensure_ascii=False), ensure_ascii=False)
- print(json_dic_escape2)
-
- # 对json_dic_escape2数据进行转义解码
- json_dic_escape_decode = json.loads(json_dic_escape2)
- print(json_dic_escape_decode)
-
- with open('text.txt', 'a', encoding='utf-8') as f:
- f.write(str(dic) + '\n')
- f.write(json_dic + '\n')
- f.write(json_dic_escape + '\n')
- f.write(json_dic_escape2 + '\n')
- f.write(json_dic_escape_decode + '\n')

运行结果:
注意:控制台有时候打印的转义符号和写入文件中的转义符号有区别!
将常见的数据类型格式转换为json格式。
示例代码:
- import json
-
- s = 'I love you!'
- lst = ['aa', 'bb', 'cc']
- dic = {"name": "dge", "age": 26}
- _set = {'aaa', 'bbb'}
-
- s_json = json.dumps(s)
- print(type(s_json), s_json)
-
- lst_json = json.dumps(lst)
- print(type(lst_json), lst_json)
-
- dic_json = json.dumps(dic)
- print(type(dic_json), dic_json)
-
- # 注意:下面写法是错误的,set集合类型的对象是不可以JSON序列化的
- # set_json = json.dumps(_set)
- # print(type(set_json), set_json)

运行结果:
注意:set集合类型的对象是不可以JSON序列化的
示例代码:
- import json
-
- _set = {'aaa', 'bbb'}
-
- # 注意:下面写法是错误的,set集合类型的对象是不可以JSON序列化的
- # set_json = json.dumps(_set)
- # print(type(set_json), set_json)
-
- # 可以先将集合类型转换为列表类型
- set_json = json.dumps(list(_set))
- print(type(set_json), set_json)
-
- _set = set(json.loads(set_json))
- print(type(_set), _set)
运行结果:
有序字典也是可以通过json直接转换为字典格式的。
示例代码:
- import json
- from collections import OrderedDict
-
-
- dic = {"name": "dgw", "address": "beijing", "date_updated": "Sun, 11 Dec 2022 13:14:25 -0000"}
- order_dic = OrderedDict(dic)
- print(type(order_dic), order_dic)
-
- order_dic_json = json.dumps(order_dic)
- print(type(order_dic_json), order_dic_json)
运行结果:
示例代码:
- import json
-
- s = 'I love you!'
- lst = ['aa', 'bb', 'cc']
- dic = {"name": "dge", "age": 26}
- _set = {'aaa', 'bbb'}
-
- # 字符串
- with open('./string.json', 'w', encoding='utf-8') as f:
- f.write(json.dumps(s))
-
- # 列表list
- with open('./list.json', 'w', encoding='utf-8') as f:
- f.write(json.dumps(lst))
-
- # 字典dict
- with open('./dict.json', 'w', encoding='utf-8') as f:
- f.write(json.dumps(dic))
-
- # 集合set
- # 注意:TypeError: Object of type set is not JSON serializable
- # 注意:set集合类型的对象是不可以JSON序列化的
- # with open('./set.json', 'w', encoding='utf-8') as f:
- # f.write(json.dumps(_set))
-
- # 将集合转换成字符串格式是可以写入文件的
- with open('./set.json', 'w', encoding='utf-8') as f:
- f.write(str(_set))

示例代码:
- import json
-
- dic = {"name": "dge", "age": 26}
-
- with open('./text.txt', 'w', encoding='utf-8') as f:
- f.write(json.dumps(dic))
-
- # 使用separators消除空白
- with open('./text2.txt', 'w', encoding='utf-8') as f:
- f.write(json.dumps(dic, separators=(',', ':')))
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。