以下是我做的对于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' : '',
'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": "",
"gender": "M",
"name": "hongten"
}
>>>
==================================================
代码部分:
==================================================
#python json #Author : Hongten
#Mailto : hongtenzone@foxmail.com
#Blog : http://www.cnblogs.com/hongten
#QQ : 648719819
#Version : 1.0
#Create : 2013-08-29 import json __doc__ = '''
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"
#} ''' print(__doc__)
print('#' * 50)
#使用json.dumps()方法对简单数据进行编码
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类型
testB = 'hongten'
dump_test = json.dumps(testB)
print(testB)
print(dump_test)
load_test = json.loads(dump_test)
print(load_test) #输出:
#hongten
#"hongten"
#hongten #排序测试
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 #测试缩进
testA = {'name' : 'hongten',
'age' : '',
'gender' : 'M'}
test_dump = json.dumps(testA, sort_keys = True, indent = 4)
print(test_dump)
#输出:
#{
# "age": "20",
# "gender": "M",
# "name": "hongten"
#}
参考资料:
http://www.cnblogs.com/coser/archive/2011/12/14/2287739.html