我的CSDN | 我的51CTO

json格式化

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

 
posted @ 2017-09-20 15:45  smileyes  阅读(912)  评论(0编辑  收藏  举报