Fork me on GitHub

python和json

  python这个语言的流行程度不用我说了,估计大家都知道吧。在字符串处理领域,json真是神一样的存在。最近一个项目中用到了,才感觉到它的威力。感觉非常有必要做一个记录和总结。

 

json是谁?

  JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写。Json模块提供了四个功能:dumps、dump、loads、load

 

实例解析

 下面一个简单的例子,解析了json的用法。

import json

test_dirct = {'bridge':"hello word",'lili':32,'horse':"yellow"}
print(type(test_dirct))

json_str = json.dumps(test_dirct)
print(json_str)
print(type(json_str))



new_dict = json.loads(json_str)
print(new_dict)
print(type(new_dict))

with open("file.json",'w') as f:
    json.dump(new_dict,f)
    print("json load success")


with open("file.json",'r') as load_f:
    load_dict = json.load(load_f)
    print(load_dict)

 

posted on 2020-04-14 11:09  虚生  阅读(565)  评论(0编辑  收藏  举报