Python3内置模块之json编码解码办法讲解
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 内置模块</p>Python3json编码解码<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/1ba972ac-8a3e-4003-a8c4-2a4d2f8cff77~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723899603&x-signature=h182WWWQcn8AFDXjrVePVgzl3UQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">简介</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式,这些特性使JSON<span style="color: black;">作为</span>理想的数据交换格式,易于人阅读和编写,<span style="color: black;">同期</span><span style="color: black;">亦</span>易于<span style="color: black;">设备</span>解析和生成,在接口数据<span style="color: black;">研发</span>和传输中非常常用。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python3中<span style="color: black;">咱们</span>利用内置模块json解码和编码JSON对象。json模块<span style="color: black;">供给</span>了四个功能:dumps、dump、loads、load</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">dumps 把数据类型转换成字符串 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">dump 把数据类型转换成字符串并存储在文件中</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">loads 把字符串转换成数据类型 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">load 把文件打开从字符串转换成数据类型</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">dumps编码</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>利用 dumps 将Python对象编码为 JSON对象 ,当然 dumps 只完<span style="color: black;">成为了</span>序列化为str,而 dump <span style="color: black;">必要</span>传文件描述符,将序列化的str<span style="color: black;">保留</span>到文件中。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">编码字典</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import json </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">odata = {www: 1, pythontab.com: 2, Python3: 3}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">jdata = json.dumps(odata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(jdata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例结果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{"www": 1, "pythontab.com": 2, "Python3": 3}</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">编码列表</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import json </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ldata = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">jdata = json.dumps(ldata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(jdata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例结果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">编码字符串</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import json </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sdata = Python3</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">jdata = json.dumps(sdata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(jdata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例结果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"Python3"</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">格式化输出JSON</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将下面的数组,转化为标准的json格式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import json </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ldata = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">jdata = json.dumps(ldata, sort_keys=True, indent=4)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(jdata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例结果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "Python3",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 100,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "Python3": 3,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "pythontab.com": 2,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "www": 1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> },</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> true</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参数解析: </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sort_keys=True <span style="color: black;">那样</span>字典的输出会以键的<span style="color: black;">次序</span>排序</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">indent=4 <span style="color: black;">暗示</span>缩进为4,<span style="color: black;">倘若</span> indent 是一个非负整数<span style="color: black;">或</span>字符串,<span style="color: black;">那样</span> JSON 数组元素和对象成员会被美化输出为该值指定的缩进等级。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">转换关系对照表</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面为Python原始类型向JSON对象的转化对照表:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python JSON dict object list, tuple array str, unicode string int, long, float number True true False false None null</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/ad9f2efd-1a6d-4629-bd17-ca0e87ce6b1d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723899603&x-signature=6XAQWqFwkbWmczN3iJnVj%2Ft5W54%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">loads解码</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>利用 loads 用于解码 JSON对象 。解码结果即与之对应的 Python对象 类型。当然 loads 只完<span style="color: black;">成为了</span>反序列化, load 只接收文件描述符,完成读取文件和反序列化。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span><span style="color: black;">咱们</span>用来解码上个示例的数据</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import json </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">jsondata = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "Python3",</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 100,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "Python3": 3,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> "pythontab.com": 2,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"www": 1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> },</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> true</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ldata = json.loads(jsondata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(type(ldata))</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(ldata)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例结果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><class list></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>看到<span style="color: black;">咱们</span>成功对上个例子中的JSON对象进行<span style="color: black;">认识</span>码,并且<span style="color: black;">最后</span>的解码结果为Python列表对象类型,符合Python对象JSON对象对照表的结果。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">总结</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python3中的json解析和编码模块json<span style="color: black;">运用</span>起来非常简单, 功能<span style="color: black;">亦</span>非常齐全,完全能够满足<span style="color: black;">咱们</span><span style="color: black;">平常</span>的需求。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,<span style="color: black;">博主</span>想说:我是一名python<span style="color: black;">研发</span>工程师,整理了一套最新的python系统学习教程,想要这些资料的<span style="color: black;">能够</span>关注私信<span style="color: black;">博主</span>“01”<span style="color: black;">就可</span>(免费分享哦)<span style="color: black;">期盼</span>能对你有所<span style="color: black;">帮忙</span>。</p>
“BS”(鄙视的缩写) 期待你更多的精彩评论,一起交流学习。 论坛外链网http://www.fok120.com/
页:
[1]