您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python开发_json_一种轻量级的数据交换格式

5b51 2022/1/14 8:24:19 python 字数 16308 阅读 645 来源 www.jb51.cc/python

python开发_json_一种轻量级的数据交换格式

概述

以下是我做的对于python中模块的demo

效果:

<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;"&gt;#</span><span style="color: #008000;"&gt;<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>:</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;[True,None]</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;[true,false,null]</span>

<span style="color: #000000;">
在json的编码过程中,会存在从python原始类型向json类型的转换过程,具体的转换
如下:

    python      </span>--><span style="color: #000000;"&gt;           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;"&gt;import</span><span style="color: #000000;"&gt; json
testB </span>= <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;hongten</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;
dump_test </span>=<span style="color: #000000;"&gt; json.dumps(testB)
</span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(testB)
</span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(dump_test)
load_test </span>=<span style="color: #000000;"&gt; json.loads(dump_test)
</span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(load_test)

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>:</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;hongten</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;"hongten"</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;hongten</span>

<span style="color: #000000;">
而json转换为python类型的时候,调用的是json.loads()方法,按照如下规则转换的:

    json        </span>--><span style="color: #000000;"&gt;           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;"&gt;import</span><span style="color: #000000;"&gt; json
data1 </span>= {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;b</span><span style="color: #800000;"&gt;'</span>:789,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;c</span><span style="color: #800000;"&gt;'</span>:456,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;a</span><span style="color: #800000;"&gt;'</span>:123<span style="color: #000000;"&gt;}
data2 </span>= {<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;a</span><span style="color: #800000;"&gt;'</span>:123,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;b</span><span style="color: #800000;"&gt;'</span>:789,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;c</span><span style="color: #800000;"&gt;'</span>:456<span style="color: #000000;"&gt;}
d1 </span>= json.dumps(data1,sort_keys=<span style="color: #000000;"&gt;True)
d2 </span>=<span style="color: #000000;"&gt; json.dumps(data2)
d3 </span>= json.dumps(data2,sort_keys=<span style="color: #000000;"&gt;True)
</span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(d1)
</span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(d2)
</span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(d3)
</span><span style="color: #0000ff;"&gt;print</span>(d1==<span style="color: #000000;"&gt;d2)
</span><span style="color: #0000ff;"&gt;print</span>(d1==<span style="color: #000000;"&gt;d3)

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>:</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;{"a": 123,"b": 789,"c": 456}</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;{"a": 123,"c": 456,"b": 789}</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;{"a": 123,"c": 456}</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;False</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;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;"&gt;#</span><span style="color: #008000;"&gt;<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>:</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;{</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    "age": "20",</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    "gender": "M",</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    "name": "hongten"</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;}</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_一种轻量级的数据交换格式所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶