I have a string which gets serialized to JSON in Javascript, and then deserialized to Java.

It looks like if the string contains a degree symbol, then I get a problem.

I could use some help in figuring out who to blame:

  • is it the Spidermonkey 1.8 implementation? (this has a JSON implementation built-in)
  • Google gson吗?
  • is it me for not doing something properly?

Here's what happens in JSDB:

js>s='15\u00f8C'
15°C
js>JSON.stringify(s)
"15°C"

I would have expected "15\u00f8C' which leads me to believe that Spidermonkey's JSON implementation isn't doing the right thing... except that the JSON homepage's syntax description (is that the spec?) says that a char can be

任何Unicode字符-

因此,它可能会按原样传递字符串,而不会将其编码为\u00f8.在这种情况下,我认为问题出在gson库上.

Can anyone help?

I suppose my workaround is to use either a different JSON library, or manually escape strings myself after calling JSON.stringify() -- but if this is a bug then I'd like to file a bug report.

推荐答案

这在这两种实现中都不是错误.不需要转义U+00B0.引用这RFC句话:

2.5. Strings

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

任何字符may都可以转义.

Escaping everything inflates the size of the data (all code points can be represented in four or fewer bytes in all Unicode transformation formats; whereas encoding them all makes them six or twelve bytes).

It is more likely that you have a text transcoding bug somewhere in your code and escaping everything in the ASCII subset masks the problem. It is a requirement of the JSON spec that all data use a Unicode encoding.

Json相关问答推荐

Android:在哪里存储json文件以便应用程序和用户都可以访问?

最新版本的Deneb在数据溢出时不支持滚动

kotlinx-serialization:如何将具有不同类型对象的JsonArray转换为同一个Class

Azure Data Factory JSON输出格式问题

Jolt需要将缺少的值设置为空并保持相同的位置

在Zig中解析JSON失败

如何避免解析 ISuperObject 类型字段中的 json 对象

将boost::beast::multibuffer转换为std::istream

使用 jq 将非统一的 json 输出转换为汇总表

如何按键过滤

JOLT JSON 将值从一对多转换为一对一

jq:用列表包装所有第一级原子值

在 Flutter 中将对象转换为可编码对象失败

使用 jq 将键值行转换为 json

当值包含ansible中的字符串时解析json值

如何对使用转换器的 Grails 服务进行单元测试?

对象序列化为 JSON(使用 Gson).如何在 UpperCamelCase 中设置字段名称?

Spring MVC:不反序列化 JSON 请求正文

从 Json 对象 Android 中获取字符串值

春天:返回@ResponseBodyResponseEntity>