import json
json_data = '{"data":[{"school":123456789,"phone":"138****0000","name":"\u5f20\u9e4f","id": null}]}'
print(json.dumps(json.loads(json_data), indent=4, sort_keys=False, ensure_ascii=False)))
搞了一个非常不优雅的版本
import json
import yaml
json_data = '{"data":[{school:123456789,"phone":"138****0000","name":"\u5f20\u9e4f",id: null}]}'
loads = yaml.load(json_data.replace(":",": "))#不能保证如果数据里存在:的情况
print json.dumps(loads, indent=4, sort_keys=False, ensure_ascii=False)
搞个第三方容错的Parse包?(个人建议)
Google了下发现有人推荐 demjson
import json
json_data = '{"data":[{school:123456789,"phone":"138****0000","name":"\u5f20\u9e4f",id: null}]}'
import demjson
loads = demjson.decode(json_data)
print json.dumps(loads, indent=4, sort_keys=False, ensure_ascii=False)
最后改了
为了把int 变成str 可以考虑用遍历对str(值) 更tricky一点是loads json 把整数当成str
print json.dumps(json.loads(json.dumps(loads), parse_int=str), indent=4, sort_keys=False, ensure_ascii=False)
至于null是不应该变成字符串的null, null 是json里一个保留值 http://www.json.org/json-zh.html 参考下json的规范