1

Is there a way to save a Unicode string into JSON that allows for Unicode codepoints to be replaced with their actual characters?

For instance, having a dict like this ported into JSON...:

dict1[u'N\u00e1utico'] = 2

...instead of having it dumped with the codepoint, could the key be dumped as the actual string?:

Náutico

Printing works fine for representing the characters, but saving I'm just lost on. Thanks.

4

2 回答 2

4

任何编写 JSON 的库都将为超出标准 ASCII 范围的字符提供 unicode 代码点,并且任何可以读取 JSON(包括浏览器)的库都将正确显示它。我不确定为什么您认为在 JSON 中表示字符串时需要重音字符,但您不应该这样做,并且作为提供代码点的交换格式是正确的行为。

于 2012-08-15T21:42:54.800 回答
1

您的意思是包含非 ASCII 字符作为原始字符,而不是等效的\u转义符?如果是这样:

>>> print json.dumps({u'N\u00e1utico': 2}, ensure_ascii= False)
{"Náutico": 2}
于 2012-08-15T22:55:58.500 回答