带有unicode的简单json转储函数 [英] simple json dumps function with unicode

查看:136
本文介绍了带有unicode的简单json转储函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用python2.4版本的简单json的示例运行

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"}'

转储函数将字节字符串替换为unicode版本.有没有办法让它不做而只返回'{"key2":"\ xe4 \ xbd \ xa0 \ xe5 \ xa5 \ xbd","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"}' ?

推荐答案

通过ensure_ascii=False,然后手动编码.

这篇关于带有unicode的简单json转储函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆