• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中字典和JSON互转操作实例

Python中字典和JSON互转操作实例

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python 字典转json,python json 字典,python json,python json模块,python json解析等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

JSON是一种轻量级的数据交换格式,各种语言都有良好的支持。字典是Python的一种数据结构。可以看成关联数组。

有些时候我们需要设计到字典转换成JSON序列化到文件,或者从文件中读取JSON。简单备忘一下。

Dict转JSON写入文件

#!/usr/bin/env python
# coding=utf-8
import json
d = {'first': 'One', 'second':2}
json.dump(d, open('/tmp/result.txt', 'w'))
</div>
写入结果

cat /tmp/result.txt
{"second": 2, "first": "One"}
</div>
读取JSON

#!/usr/bin/env python
# coding=utf-8
import json
d = json.load(open('/tmp/result.txt','r'))
print d, type(d)
</div>
运行结果

{u'second': 2, u'first': u'One'} <type 'dict'>
</div>

其他

PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat

在线json压缩/转义工具:

http://tools.jb51.net/code/json_yasuo_trans

C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

</div>

您可能想查找下面的文章:

  • Python处理json字符串转化为字典的简单实现
  • Python处理json字符串转化为字典的简单实现
  • Python中字典和JSON互转操作实例
  • python中将字典转换成其json字符串

相关文章

  • 使用python分析git log日志示例
  • python 简单的多线程链接实现代码
  • 讲解Python中的标识运算符
  • python 随机数生成的代码的详细分析
  • Python NumPy库安装使用笔记
  • TensorFlow——Checkpoint为模型添加检查点的实例
  • Python字符和字符值(ASCII或Unicode码值)转换方法
  • python自定义类并使用的方法
  • python使用xmlrpclib模块实现对百度google的ping功能
  • python使用urlparse分析网址中域名的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python中常用检测字符串相关函数汇总
    • Python二叉搜索树与双向链表转换实现方法
    • Python发送form-data请求及拼接form-data内容的方法
    • Python的Flask站点中集成xhEditor文本编辑器的教程
    • 使用python调用zxing库生成二维码图片详解
    • python BeautifulSoup设置页面编码的方法
    • python 文件操作api(文件操作函数)
    • python正则表达式去掉数字中的逗号(python正则匹配逗号)
    • python抓取京东商城手机列表url实例代码
    • Python中http请求方法库汇总

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有