1.将json格式化输出
di = {"a":"aa", "b":"bb", "c":[1,2,3]}
print json.dumps(di, indent=4)
--------------
{
"a": "aa",
"c": [
1,
2,
3
],
"b": "bb"
}
2.将格式化的json字符串还原
json_demo = """{
"a": "aa",
"c": [
1,
2,
3
],
"b": "bb"
}"""
str_demo = json_demo.replace(' ', '').replace('\n', '').replace('\t', '')
print(str_demo)
----------------------------------
{"a":"aa","c":[1,2,3],"b":"bb"}
3.备注
json.loads