用于 JSON 的内容类型标头是什么?“应用程序/json;字符集=utf-8”还是“应用程序/json”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19814908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:00:37  来源:igfitidea点击:

What content type header to use for JSON? "application/json; charset=utf-8 " or "application/json"?

jsonhttpspring-mvc

提问by Bobo

It seems that the JSON default encoding is UTF-8 and Spring MVC returns application/json; charset=utf-8by default, which is hard to change.

貌似JSON默认编码是UTF-8,Spring MVCapplication/json; charset=utf-8默认返回 ,很难改。

回答by Joachim Sauer

According to RFC 4627

根据RFC 4627

JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

JSON 文本应以 Unicode 编码。默认编码为 UTF-8。

It goes on to describe how the different UTF-* encoding are to be detecting, suggesting that no other encodings are supported.

它继续描述如何检测不同的 UTF-* 编码,表明不支持其他编码。

"SHALL" represents an absolute requirement here (see RFC 2119).

“SHALL”在这里代表绝对要求(参见RFC 2119)。

And there really is no reason to use a non-UTF encoding with JSON (as anything that can handle JSON can definitelyhandle UTF-8).

而且真的没有理由对 JSON 使用非 UTF 编码(因为任何可以处理 JSON 的东西肯定可以处理 UTF-8)。

回答by Julian Reschke

Returning a charset parameter on application/json is incorrect, as that media type does not define a charset parametert. See https://greenbytes.de/tech/webdav/rfc8259.html#rfc.section.11, last sentence:

在 application/json 上返回字符集参数是不正确的,因为该媒体类型没有定义字符集参数。见https://greenbytes.de/tech/webdav/rfc8259.html#rfc.section.11,最后一句:

Note: No "charset" parameter is defined for this registration. Adding one really has no effect on compliant recipients.

注意:没有为此注册定义“字符集”参数。添加一个确实对合规的收件人没有影响。