python 使用json格式转换
什么是json:
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。
JSON建构于两种结构:
“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
使用json的四种方式dumps、dump、loads、load
使用简单的json.dumps方法对简单数据类型进行编码
def test():
import json
info = [{'a':'1','b':'2','c':'3','d':'4','f':'5'}]
data = json.dumps(info, sort_keys=True, indent=4, separators=(',', ': '))
print(data)
def test1():
import json
jsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
text = json.loads(jsonData)
print(jsonData)
print(text)
保存json格式文件的方式
1 2 3 4 5 | def write_file(): import json a = { 'a' : '1' , 'b' : '2' , 'c' : '3' } with open ( "test.json" , "w" , encoding = 'utf-8' ) as f: f.write(json.dumps(a, indent = 4 )) ##indent 缩进4个 |
打开json文件转换python的方式
1 2 3 4 5 6 7 | def open_file(): import json with open ( "test.json" , "r" , encoding = 'utf-8' ) as f: json_file = json.loads(f.read()) f.seek( 0 ) print (json_file[ 'b' ]) return json_file |
使用load打开json文件方式
1 2 3 4 5 6 7 | def load_file(): import json with open ( "test.json" , "r" , encoding = 'utf-8' ) as f: json_file = json.load(f) f.seek( 0 ) print (json_file[ 'a' ]) return json_file |
获取字典的值
通过输出的结果可以看出,简单类型通过encode之后跟其原始的repr()输出结果非常相似,但是有些数据类型进行了改变,例如上例中的元组则转换为了列表。在json的编码过程中,会存在从python原始类型向json类型的转化过程,具体的转化对照如下:
json.dumps()方法返回了一个str对象encodedjson,我们接下来在对encodedjson进行decode,得到原始数据,需要使用的json.loads()函数:
1 2 3 4 5 6 7 8 9 | def test2(): import json obj = [[ 1 , 2 , 3 ], 123 , 123.123 , 'abc' ,{ 'key1' :( 1 , 2 , 3 ), 'key2' :( 4 , 5 , 6 )}] encodedjson = json.dumps(obj) decodejson = json.loads(encodedjson) print ( repr (obj)) print (encodedjson) print (decodejson[ 4 ][ 'key1' ]) print (decodejson) |
对比两个参数
上例中,本来data1和data2数据应该是一样的,但是由于dict存储的无序特性,造成两者无法比较。因此两者可以通过排序后的结果进行存储就避免了数据比较不一致的情况发生,但是排序后再进行存储,系统必定要多做一些事情,也一定会因此造成一定的性能消耗,所以适当排序是很重要的
1 2 3 4 5 6 7 8 9 10 11 12 | def sort_json(): import json data2 = { 'b' : 789 , 'c' : 456 , 'a' : 123 } data1 = { 'a' : 123 , 'b' : 789 , 'c' : 456 } d1 = json.dumps(data1, sort_keys = True ) #转换时进行排序 d2 = json.dumps(data2) d3 = json.dumps(data2,sort_keys = True ) print (d1) print (d2) print (d3) print (d1 = = d2) print (d1 = = d3) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南