python开发_json_一种轻量级的数据交换格式
以下是我做的对于python中json模块的demo
运行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> JSON(JavaScript Object Notation)是一种轻量级的数据交换 格式。易于人阅读和编写,同时也易于机器解析和生成。 在python中,json模块提供的dumps()方法可以对简单的数据进行编码: import json obj = [['a', 'b', 'c'], 1, 3, 4, 'good', 'boy',(88, 42, 'hongten'), {'name' : 'hongten'}] encodedjson = json.dumps(obj) print(repr(obj)) print(encodedjson) #输出: #[['a', 'b', 'c'], 1, 3, 4, 'good', 'boy', (88, 42, 'hongten'), {'name': 'hongten'}] #[["a", "b", "c"], 1, 3, 4, "good", "boy", [88, 42, "hongten"], {"name": "hongten"}] objA = [True, False, None] encodedjsonA = json.dumps(objA) print(repr(objA)) print(encodedjsonA) #输出: #[True, False, None] #[true, false, null] 在json的编码过程中,会存在从python原始类型向json类型的转换过程,具体的转换 如下: python --> json dict object list,tuple array str,unicode string int,long,float number True true False false None null json转换为python数据类型: import json testB = 'hongten' dump_test = json.dumps(testB) print(testB) print(dump_test) load_test = json.loads(dump_test) print(load_test) #输出: #hongten #"hongten" #hongten 而json转换为python类型的时候,调用的是json.loads()方法,按照如下规则转换的: json --> python object dict array list string str number(int) int number(real) float true True false False null None 排序功能使得存储的数据更加有利于观察,也使得对json输出的对象进行比较: import json data1 = {'b':789,'c':456,'a':123} data2 = {'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) #输出: #{"a": 123, "b": 789, "c": 456} #{"a": 123, "c": 456, "b": 789} #{"a": 123, "b": 789, "c": 456} #False #True indent参数是缩进的意思: import json testA = {'name' : 'hongten', 'age' : '20', 'gender' : 'M'} test_dump = json.dumps(testA, sort_keys = True, indent = 4) print(test_dump) #输出: #{ # "age": "20", # "gender": "M", # "name": "hongten" #} ################################################## [['a', 'b', 'c'], 1, 3, 4, 'good', 'boy', (88, 42, 'hongten'), {'name': 'hongten'}] [["a", "b", "c"], 1, 3, 4, "good", "boy", [88, 42, "hongten"], {"name": "hongten"}] [True, False, None] [true, false, null] hongten "hongten" hongten {"a": 123, "b": 789, "c": 456} {"b": 789, "c": 456, "a": 123} {"a": 123, "b": 789, "c": 456} False True { "age": "20", "gender": "M", "name": "hongten" } >>>
==================================================
代码部分:
==================================================
1 #python json 2 3 #Author : Hongten 4 #Mailto : hongtenzone@foxmail.com 5 #Blog : http://www.cnblogs.com/hongten 6 #QQ : 648719819 7 #Version : 1.0 8 #Create : 2013-08-29 9 10 import json 11 12 __doc__ = ''' 13 JSON(JavaScript Object Notation)是一种轻量级的数据交换 14 格式。易于人阅读和编写,同时也易于机器解析和生成。 15 16 在python中,json模块提供的dumps()方法可以对简单的数据进行编码: 17 import json 18 19 obj = [['a', 'b', 'c'], 1, 3, 4, 'good', 'boy',(88, 42, 'hongten'), {'name' : 'hongten'}] 20 encodedjson = json.dumps(obj) 21 print(repr(obj)) 22 print(encodedjson) 23 24 #输出: 25 #[['a', 'b', 'c'], 1, 3, 4, 'good', 'boy', (88, 42, 'hongten'), {'name': 'hongten'}] 26 #[["a", "b", "c"], 1, 3, 4, "good", "boy", [88, 42, "hongten"], {"name": "hongten"}] 27 28 objA = [True, False, None] 29 encodedjsonA = json.dumps(objA) 30 print(repr(objA)) 31 print(encodedjsonA) 32 33 #输出: 34 #[True, False, None] 35 #[true, false, null] 36 37 在json的编码过程中,会存在从python原始类型向json类型的转换过程,具体的转换 38 如下: 39 40 python --> json 41 dict object 42 list,tuple array 43 str,unicode string 44 int,long,float number 45 True true 46 False false 47 None null 48 49 json转换为python数据类型: 50 import json 51 testB = 'hongten' 52 dump_test = json.dumps(testB) 53 print(testB) 54 print(dump_test) 55 load_test = json.loads(dump_test) 56 print(load_test) 57 58 #输出: 59 #hongten 60 #"hongten" 61 #hongten 62 63 而json转换为python类型的时候,调用的是json.loads()方法,按照如下规则转换的: 64 65 json --> python 66 object dict 67 array list 68 string str 69 number(int) int 70 number(real) float 71 true True 72 false False 73 null None 74 75 排序功能使得存储的数据更加有利于观察,也使得对json输出的对象进行比较: 76 import json 77 data1 = {'b':789,'c':456,'a':123} 78 data2 = {'a':123,'b':789,'c':456} 79 d1 = json.dumps(data1,sort_keys=True) 80 d2 = json.dumps(data2) 81 d3 = json.dumps(data2,sort_keys=True) 82 print(d1) 83 print(d2) 84 print(d3) 85 print(d1==d2) 86 print(d1==d3) 87 88 #输出: 89 #{"a": 123, "b": 789, "c": 456} 90 #{"a": 123, "c": 456, "b": 789} 91 #{"a": 123, "b": 789, "c": 456} 92 #False 93 #True 94 95 indent参数是缩进的意思: 96 import json 97 testA = {'name' : 'hongten', 98 'age' : '20', 99 'gender' : 'M'} 100 test_dump = json.dumps(testA, sort_keys = True, indent = 4) 101 print(test_dump) 102 103 #输出: 104 #{ 105 # "age": "20", 106 # "gender": "M", 107 # "name": "hongten" 108 #} 109 110 111 ''' 112 113 print(__doc__) 114 print('#' * 50) 115 #使用json.dumps()方法对简单数据进行编码 116 obj = [['a', 'b', 'c'], 1, 3, 4, 'good', 'boy',(88, 42, 'hongten'), {'name' : 'hongten'}] 117 encodedjson = json.dumps(obj) 118 print(repr(obj)) 119 print(encodedjson) 120 121 #[['a', 'b', 'c'], 1, 3, 4, 'good', 'boy', (88, 42, 'hongten'), {'name': 'hongten'}] 122 #[["a", "b", "c"], 1, 3, 4, "good", "boy", [88, 42, "hongten"], {"name": "hongten"}] 123 124 125 objA = [True, False, None] 126 encodedjsonA = json.dumps(objA) 127 print(repr(objA)) 128 print(encodedjsonA) 129 130 #[True, False, None] 131 #[true, false, null] 132 133 #测试json转换为python类型 134 testB = 'hongten' 135 dump_test = json.dumps(testB) 136 print(testB) 137 print(dump_test) 138 load_test = json.loads(dump_test) 139 print(load_test) 140 141 #输出: 142 #hongten 143 #"hongten" 144 #hongten 145 146 147 #排序测试 148 data1 = {'b':789,'c':456,'a':123} 149 data2 = {'a':123,'b':789,'c':456} 150 d1 = json.dumps(data1,sort_keys=True) 151 d2 = json.dumps(data2) 152 d3 = json.dumps(data2,sort_keys=True) 153 print(d1) 154 print(d2) 155 print(d3) 156 print(d1==d2) 157 print(d1==d3) 158 159 #输出: 160 #{"a": 123, "b": 789, "c": 456} 161 #{"a": 123, "c": 456, "b": 789} 162 #{"a": 123, "b": 789, "c": 456} 163 #False 164 #True 165 166 #测试缩进 167 testA = {'name' : 'hongten', 168 'age' : '20', 169 'gender' : 'M'} 170 test_dump = json.dumps(testA, sort_keys = True, indent = 4) 171 print(test_dump) 172 #输出: 173 #{ 174 # "age": "20", 175 # "gender": "M", 176 # "name": "hongten" 177 #}
参考资料:
http://www.cnblogs.com/coser/archive/2011/12/14/2287739.html