dumps unicode

sample code:

import json
json_string = json.dumps(“ברי צקלה”)
print json_string
“\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4”
The problem: it’s not human readable. My (smart) users want to verify or even edit text files with JSON dumps. (and i’d rather not use XML)

Is there a way to serialize objects into utf-8 json string (instead of \uXXXX ) ?

this doesn’t help:

output = json_string.decode(‘string-escape’)
“\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4”
this works, but if any sub-objects is a python-unicode and not utf-8, it’ll dump garbage:

ok:

s= json.dumps( “ברי צקלה”, ensure_ascii=False)
print json.loads(s)
ברי צקלה

NOT ok:

d={ 1: “ברי צקלה”, 2: u"ברי צקלה" }
print d
{1: ‘\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94’,
2: u’\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94’}
s = json.dumps( d, ensure_ascii=False, encoding=‘utf8’)
print json.loads(s)[‘1’]
ברי צקלה
print json.loads(s)[‘2’]
××¨× ×¦×§××

猜你喜欢

转载自blog.csdn.net/sinat_26114733/article/details/88980941
今日推荐