Python:json解析
#!/usr/bin/env python# coding:UTF-8"""@version: python3.x@author:曹新健@contact: 617349013@qq.com@software: PyCharm@file: json数据解析.py@time: 2018/9/17 14:32"""import jsonjson
·
#!/usr/bin/env python
# coding:UTF-8
"""
@version: python3.x
@author:曹新健
@contact: 617349013@qq.com
@software: PyCharm
@file: json数据解析.py
@time: 2018/9/17 14:32
"""
import json
jsonStr = '{"name":"cxj","age":18,"hobby":["money","power","girl"]}'
#将json类型的字符串转为python中的数据类型对象
jsonData = json.loads(jsonStr)
print(jsonData)
print(type(jsonData))
print(jsonData["hobby"])
#将python中的数据类型对象转为json类型的字符串
jsonStr2 = json.dumps(jsonData)
print(type(jsonStr2))
print(jsonStr2)
#读取本地json文件
path = "1.json"
with open(path,"r") as f:
data = json.load(f)
print(data)
print(type(data)) #dict类型
#写本地json文件
dictData= {"name":"cxj","age":18,"hobby":["money","power","girl"]}
path2 = "2.json"
with open(path2,"w") as f:
json.dump(dictData,f)
更多推荐
已为社区贡献51条内容
所有评论(0)