最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python操作json的例子
时间:2014-07-31 编辑:简简单单 来源:一聚教程网
首先操作一下python中的字典,首先是空字典eth,在其中添加数据eth0,eth1并对应两个ip
>>> eth = {}
>>> eth['eth0'] = ’192.168.2.12′
>>> print eth
{‘eth0′: ’192.168.2.12′}
>>> eth['eth1'] = ’223.5.5.5′
>>> print eth
{‘eth1′: ’223.5.5.5′, ‘eth0′: ’192.168.2.12′}
json与python中dict互相转换,把dict转换成json-使用json.dumps(),将json转换为dict-使用json.loads()
>>> import json
>>> ethjson = json.dumps(eth)
>>> type(ethjson)
>>> print ethjson
{“eth1″: “223.5.5.5″, “eth0″: “192.168.2.12″}
>>> ethdict = json.loads(ethjson)
>>> type(ethdict)
>>> print ethdict
{u’eth1′: u’223.5.5.5′, u’eth0′: u’192.168.2.12′}
>>> print ethdict['eth0'], ethdict['eth1']
192.168.2.12 223.5.5.5
判断json里是否有某个key
if ‘text’ in post['caption'].keys():
if ‘text’ in post['caption']:
if ‘ipv4′ in output['ansible_facts'][ansible_int]:
-
上一个: Tornado文件图片上传例子
相关文章
- Python json 错误xx is not JSON serializable解决办法 06-29
- JSON与JS对象的区别与对比 05-11
- net.sf.json.JSONObject 为null 的判断方法 04-13
- java怎么使用json? 02-20
- python对json的操作详解 01-05
- 用jsonp实现搜索框功能的例子 10-28