发布时间:2019-09-16 07:35:21编辑:auto阅读(1526)
编码:把一个Python对象编码转换成Json字符串 json.dumps()
解码:把Json格式字符串解码转换成Python对象 json.loads()
In [1]: import json In [2]: dic = { 'str': 'this is a string', 'list': [1, 2, 'a', 'b'], 'sub_dic': { 'sub_str': 'this is sub str', 'sub_list': [1, 2, 3] }, 'end': 'end' } In [3]: type(dic) Out[3]: dict In [5]: json_obj=json.dump(dic) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-5-1959d613a6c1> in <module>() ----> 1 json_obj=json.dump(dic) TypeError: dump() takes at least 2 arguments (1 given) In [6]: json_obj=json.dumps(dic) In [8]: type(json_obj) Out[8]: str In [9]: print json_obj {"sub_dic": {"sub_str": "this is sub str", "sub_list": [1, 2, 3]}, "end": "end", "list": [1, 2, "a", "b"], "str": "this is a string"} In [10]: dic1=json.loads(json_obj) In [11]: type(dic1) Out[11]: dict In [12]: print dic1 {u'end': u'end', u'list': [1, 2, u'a', u'b'], u'sub_dic': {u'sub_str': u'this is sub str', u'sub_list': [1, 2, 3]}, u'str': u'this is a string'} In [13]: print dic {'sub_dic': {'sub_str': 'this is sub str', 'sub_list': [1, 2, 3]}, 'end': 'end', 'list': [1, 2, 'a', 'b'], 'str': 'this is a string'}
参考:https://docs.python.org/dev/library/json.html
上一篇: mac安装python-ldap
下一篇: python 加载excel报错
47842
46390
37281
34733
29313
25973
24914
19951
19544
18030
5792°
6413°
5927°
5961°
7064°
5911°
5944°
6438°
6404°
7778°