-
python 有专门针对 json 操作的函数
-
python 输出 json 字符串
#!/usr/bin/python3 import json mytest_js = { "a" : 1, "b" : 2 } test = json.dumps({'4': 5, '6': 7}); # 合成不换行的字符串 json 数据 test1 = json.dumps({'4': 5, '6': 7}, sort_keys = True, indent = 4, separators = (',', ':')); # 合成有换行的字符串数据 mytest_jsson = json.dumps(mytest_js); # 解析出 json 格式的字符串 print(test); print(""); print(test1); print(""); print(mytest_jsson);
// 输出 {"4": 5, "6": 7} { "4":5, "6":7 } {"a": 1, "b": 2}
-
json 字符串 转化为 python 对象
test1 = json.dumps({'4': 5, '6': 7}, sort_keys = True, indent = 4, separators = (',', ':')); # 先合成为字符串 text = json.loads(test1); # 后转化为对象输出 print(text); print(text['4']); print(text['6']);
{'4': 5, '6': 7} 5 7
-
参考
https://docs.python.org/2/library/json.html http://www.runoob.com/python/python-json.html http://www.runoob.com/python3/python3-json.html
时间: 2024-11-07 03:50:45