概述
效果:
<span style="color: #000000;">
objA =<span style="color: #000000;"> [True,False,None]
encodedjsonA =<span style="color: #000000;"> json.dumps(objA)
<span style="color: #0000ff;">print<span style="color: #000000;">(repr(objA))
<span style="color: #0000ff;">print<span style="color: #000000;">(encodedjsonA)
</span><span style="color: #008000;">#</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>:</span>
<span style="color: #008000;">#</span><span style="color: #008000;">[True,None]</span>
<span style="color: #008000;">#</span><span style="color: #008000;">[true,false,null]</span>
<span style="color: #000000;">
在json的编码过程中,会存在从python原始类型向json类型的转换过程,具体的转换
如下:
python </span>--><span style="color: #000000;"> json
dict object
list,tuple array
str,unicode string
int,long,float number
True true
False false
None null
json转换为python数据类型:
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> json
testB </span>= <span style="color: #800000;">'</span><span style="color: #800000;">hongten</span><span style="color: #800000;">'</span><span style="color: #000000;">
dump_test </span>=<span style="color: #000000;"> json.dumps(testB)
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">(testB)
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">(dump_test)
load_test </span>=<span style="color: #000000;"> json.loads(dump_test)
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">(load_test)
</span><span style="color: #008000;">#</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>:</span>
<span style="color: #008000;">#</span><span style="color: #008000;">hongten</span>
<span style="color: #008000;">#</span><span style="color: #008000;">"hongten"</span>
<span style="color: #008000;">#</span><span style="color: #008000;">hongten</span>
<span style="color: #000000;">
而json转换为python类型的时候,调用的是json.loads()方法,按照如下规则转换的:
json </span>--><span style="color: #000000;"> python
object dict
array list
string str
number(int) int
number(real) float
true True
false False
null None
排序<a href="https://www.jb51.cc/tag/gongneng/" target="_blank" class="keywords">功能</a>使得存储的数据更加有利于观察,也使得对json<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>的对象进行比较:
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> json
data1 </span>= {<span style="color: #800000;">'</span><span style="color: #800000;">b</span><span style="color: #800000;">'</span>:789,<span style="color: #800000;">'</span><span style="color: #800000;">c</span><span style="color: #800000;">'</span>:456,<span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span>:123<span style="color: #000000;">}
data2 </span>= {<span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span>:123,<span style="color: #800000;">'</span><span style="color: #800000;">b</span><span style="color: #800000;">'</span>:789,<span style="color: #800000;">'</span><span style="color: #800000;">c</span><span style="color: #800000;">'</span>:456<span style="color: #000000;">}
d1 </span>= json.dumps(data1,sort_keys=<span style="color: #000000;">True)
d2 </span>=<span style="color: #000000;"> json.dumps(data2)
d3 </span>= json.dumps(data2,sort_keys=<span style="color: #000000;">True)
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">(d1)
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">(d2)
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">(d3)
</span><span style="color: #0000ff;">print</span>(d1==<span style="color: #000000;">d2)
</span><span style="color: #0000ff;">print</span>(d1==<span style="color: #000000;">d3)
</span><span style="color: #008000;">#</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>:</span>
<span style="color: #008000;">#</span><span style="color: #008000;">{"a": 123,"b": 789,"c": 456}</span>
<span style="color: #008000;">#</span><span style="color: #008000;">{"a": 123,"c": 456,"b": 789}</span>
<span style="color: #008000;">#</span><span style="color: #008000;">{"a": 123,"c": 456}</span>
<span style="color: #008000;">#</span><span style="color: #008000;">False</span>
<span style="color: #008000;">#</span><span style="color: #008000;">True</span>
<span style="color: #000000;">
indent参数是缩进的意思:
<span style="color: #0000ff;">import<span style="color: #000000;"> json
testA = {<span style="color: #800000;">'<span style="color: #800000;">name<span style="color: #800000;">' : <span style="color: #800000;">'<span style="color: #800000;">hongten<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">age<span style="color: #800000;">' : <span style="color: #800000;">'<span style="color: #800000;">20<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">gender<span style="color: #800000;">' : <span style="color: #800000;">'<span style="color: #800000;">M<span style="color: #800000;">'<span style="color: #000000;">}
test_dump = json.dumps(testA,sort_keys = True,indent = 4<span style="color: #000000;">)
<span style="color: #0000ff;">print<span style="color: #000000;">(test_dump)
</span><span style="color: #008000;">#</span><span style="color: #008000;"><a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>:</span>
<span style="color: #008000;">#</span><span style="color: #008000;">{</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> "age": "20",</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> "gender": "M",</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> "name": "hongten"</span>
<span style="color: #008000;">#</span><span style="color: #008000;">}</span>
<span style="color: #008000;">#<span style="color: #008000;">#################################################
[[<span style="color: #800000;">'<span style="color: #800000;">a<span style="color: #800000;">',{<span style="color: #800000;">'<span style="color: #800000;">name<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">hongten<span style="color: #800000;">'<span style="color: #000000;">}]
[[<span style="color: #800000;">"<span style="color: #800000;">a<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">b<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">c<span style="color: #800000;">"],<span style="color: #800000;">"<span style="color: #800000;">good<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">boy<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">hongten<span style="color: #800000;">"],{<span style="color: #800000;">"<span style="color: #800000;">name<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">hongten<span style="color: #800000;">"<span style="color: #000000;">}]
[True,None]
[true,null]
hongten
<span style="color: #800000;">"<span style="color: #800000;">hongten<span style="color: #800000;">"<span style="color: #000000;">
hongten
{<span style="color: #800000;">"<span style="color: #800000;">a<span style="color: #800000;">": 123,<span style="color: #800000;">"<span style="color: #800000;">b<span style="color: #800000;">": 789,<span style="color: #800000;">"<span style="color: #800000;">c<span style="color: #800000;">": 456<span style="color: #000000;">}
{<span style="color: #800000;">"<span style="color: #800000;">b<span style="color: #800000;">": 789,<span style="color: #800000;">"<span style="color: #800000;">c<span style="color: #800000;">": 456,<span style="color: #800000;">"<span style="color: #800000;">a<span style="color: #800000;">": 123<span style="color: #000000;">}
{<span style="color: #800000;">"<span style="color: #800000;">a<span style="color: #800000;">": 123,<span style="color: #800000;">"<span style="color: #800000;">c<span style="color: #800000;">": 456<span style="color: #000000;">}
False
True
{
<span style="color: #800000;">"<span style="color: #800000;">age<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">20<span style="color: #800000;">"<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">gender<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">M<span style="color: #800000;">"<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">name<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">hongten<span style="color: #800000;">"<span style="color: #000000;">
}
>>>
代码部分:
总结
以上是编程之家为你收集整理的python开发_json_一种轻量级的数据交换格式全部内容,希望文章能够帮你解决python开发_json_一种轻量级的数据交换格式所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧