开发者

How to decode JSON string to string, not unicode

I'm trying to decode a json of a dictionary with strings as keys. The result is a dictionary with unicode keys. What is the best way to decode to a dictionary with string keys? Better: how do I prevent that strings are decoded into unicode strings? Off course I can loop afterwards...

What happens:

>>> import simplejson
>>> simplejson.loads('{"bar":["baz", null, 1.0, 2]}')
{u'bar': [u'baz', None, 1.0, 2]}开发者_如何学JAVA
>>> simplejson.loads('"bar"')
u'bar'

Desired behaviour:

>>> import simplejson
>>> simplejson.loads('{"bar":["baz", null, 1.0, 2]}', ...?)
{'bar': ['baz', None, 1.0, 2]}
>>> simplejson.loads('"bar"', ..?)
'bar'


You can't. Encode the strings after loading. Or even better, fix the rest of the code so that it doesn't fall over when using unicode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜