simple json dumps function with unicode

社会主义新天地 提交于 2019-12-07 20:35:11

问题


Here is a sample run of simple json using python2.4 version

>>> 
>>> orig='{"key1":"Val", "key2":"val2"}'
>>> origDict = simplejson.loads(orig)
>>> origDict
{'key2': 'val2', 'key1': 'Val'}
>>> origDict['key2'] = '\xe4\xbd\xa0\xe5\xa5\xbd'
>>> simplejson.dumps(origDict)
'{"key2": "\\u4f60\\u597d", "key1": "Val"}'

The dumps functions is replacing the byte string with the unicode version. Is there a way to make it not do that and just return '{"key2": "\xe4\xbd\xa0\xe5\xa5\xbd", "key1": "Val"}' ?


回答1:


Pass ensure_ascii=False and encode manually after.



来源:https://stackoverflow.com/questions/9883464/simple-json-dumps-function-with-unicode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!