[转载] python中json文件处理涉及的四个函数json.dumps()和json.loads()、json.dump()和json.load()的区分

转载出处:https://www.cnblogs.com/xiaomingzaixian/p/7286793.html

一、概念理解

1、json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)
  (1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串)
  (2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)

2、json.dump()和json.load()主要用来读写json文件函数

二、代码测试

1.py

复制代码
1 import json
2 
3 # json.dumps()函数的使用,将字典转化为字符串
4 dict1 = {"age": "12"}
5 json_info = json.dumps(dict1)
6 print("dict1的类型:"+str(type(dict1)))
7 print("通过json.dumps()函数处理:")
8 print("json_info的类型:"+str(type(json_info)))
复制代码

运行截图:

 2.py

复制代码
1 import json
2 
3 # json.loads函数的使用,将字符串转化为字典
4 json_info = '{"age": "12"}'
5 dict1 = json.loads(json_info)
6 print("json_info的类型:"+str(type(json_info)))
7 print("通过json.dumps()函数处理:")
8 print("dict1的类型:"+str(type(dict1)))
复制代码

运行截图:

 

3.py

复制代码
1 import json
2 
3 # json.dump()函数的使用,将json信息写进文件
4 json_info = "{'age': '12'}"
5 file = open('1.json','w',encoding='utf-8')
6 json.dump(json_info,file)
复制代码

运行截图(1.json文件):

4.py

复制代码
1 import json
2 
3 # json.load()函数的使用,将读取json信息
4 file = open('1.json','r',encoding='utf-8')
5 info = json.load(file)
6 print(info)
复制代码

运行截图:

 

标签: python-json
3
0
« 上一篇:python中的URL编码和解码
» 下一篇:python抓取网页中的动态数据
	</div>
	<p class="postfoot">
		posted on <span id="post-date">2017-08-04 19:26</span> <a href="https://www.cnblogs.com/xiaomingzaixian/">小明在线</a> 阅读(<span id="post_view_count">22171</span>) 评论(<span id="post_comment_count">2</span>)  <a href="https://i.cnblogs.com/EditPosts.aspx?postid=7286793" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(7286793);return false;">收藏</a>
	</p>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=241436,cb_entryId=7286793,cb_blogApp=currentBlogApp,cb_blogUserGuid='63bdff51-7e49-e511-b908-9dcfd8948a71',cb_entryCreatedDate='2017/8/4 19:26:00';loadViewCount(cb_entryId);var cb_postType=1;</script>

</div><a name="!comments"></a><div id="blog-comments-placeholder"><div id="comments_pager_top"></div>

评论

		<div class="post">
			<h2>
				<a href="#4042848" class="layer">#1楼</a><a name="4042848" id="comment_anchor_4042848"></a>
				&nbsp;&nbsp;<span class="comment_actions"><a href="javascript:void(0);" onclick="return ReplyComment(4042848,'VRRIU4hDfaSNgLplakWe1+qRNlMfKlXFvUX7t3Alya4We/ozp62JMA==')">回复</a><a href="javascript:void(0);" onclick="return QuoteComment(4042848,'VRRIU4hDfaSNgLplakWe1+qRNlMfKlXFvUX7t3Alya4We/ozp62JMA==')">引用</a></span>
			</h2>
			<div id="comment_body_4042848" class="blog_comment_body">写的很详细,借用了,谢谢</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4042848,'Digg',this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(4042848,'Bury',this)">反对(0)</a></div><span id="comment_4042848_avatar" style="display:none;">http://pic.cnblogs.com/face/1424202/20180705134532.png</span>
			<div class="postfoot">
				 <span class="comment_date">2018-08-14 16:54</span> | <a id="a_comment_author_4042848" href="https://www.cnblogs.com/ylpaj/" target="_blank">ylpaj</a> <a href="http://msg.cnblogs.com/send/ylpaj" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
		</div>
	
		<div class="post">
			<h2>
				<a href="#4089913" class="layer">#2楼</a><a name="4089913" id="comment_anchor_4089913"></a><span id="comment-maxId" style="display:none;">4089913</span><span id="comment-maxDate" style="display:none;">2018/10/15 14:56:36</span>
				&nbsp;&nbsp;<span class="comment_actions"><a href="javascript:void(0);" onclick="return ReplyComment(4089913,'Hh0jJzS4Mq0nDcfT9V79gHva7/67fKqgMpEp+eW7xfW5WLs/3wAbtg==')">回复</a><a href="javascript:void(0);" onclick="return QuoteComment(4089913,'Hh0jJzS4Mq0nDcfT9V79gHva7/67fKqgMpEp+eW7xfW5WLs/3wAbtg==')">引用</a></span>
			</h2>
			<div id="comment_body_4089913" class="blog_comment_body">干的漂亮!</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4089913,'Digg',this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(4089913,'Bury',this)">反对(0)</a></div><span id="comment_4089913_avatar" style="display:none;">http://pic.cnblogs.com/face/1031557/20170409210949.png</span>
			<div class="postfoot">
				 <span class="comment_date">2018-10-15 14:56</span> | <a id="a_comment_author_4089913" href="https://www.cnblogs.com/zyber/" target="_blank">鹿往森处走</a> <a href="http://msg.cnblogs.com/send/%E9%B9%BF%E5%BE%80%E6%A3%AE%E5%A4%84%E8%B5%B0" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
		</div>
posted @ 2018-10-30 21:55  surimj  阅读(193)  评论(0编辑  收藏  举报