JSON和转义字符 [英] JSON and escaping characters

查看:450
本文介绍了JSON和转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,该字符串在Javascript中序列化为JSON,然后反序列化为Java.

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:

  • 这是Spidermonkey 1.8的实现吗? (这具有内置的JSON实现)
  • Google gson 吗?
  • 是我做不正确的事吗?
  • is it the Spidermonkey 1.8 implementation? (this has a JSON implementation built-in)
  • is it Google gson?
  • is it me for not doing something properly?

这是JSDB中发生的事情:

Here's what happens in JSDB:

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

我会期望"15\u00f8C',这使我相信Spidermonkey的JSON实现没有做正确的事...除了

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字符- 除了"或或" 控制字符"

any-Unicode-character- except-"-or-\-or- control-character"

所以也许它按原样传递字符串而不将其编码为\ u00f8 ...在这种情况下,我认为问题出在gson库.

so maybe it passes the string along as-is without encoding it as \u00f8... in which case I would think the problem is with the gson library.

任何人都可以帮忙吗?

我想我的解决方法是使用其他JSON库,或者在调用JSON.stringify()之后亲自自己对字符串进行转义-但是,如果这是一个错误,那么我想提交一个错误报告.

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 :

This is not a bug in either implementation. There is no requirement to escape U+00B0. To quote the RFC:

2.5.字符串

字符串的表示形式是 类似于C语言中使用的约定 编程语言家族.一种 字符串以引号开头和结尾 分数.所有Unicode字符都可能是 放在引号内 除了必须是 转义:引号,反向 固相线和控制字符 (U + 0000到U + 001F).

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).

任何字符 都可以转义.

对所有内容进行转义会增大数据的大小(在所有Unicode转换格式中,所有代码点都可以用四个或更少的字节表示;而对它们进行编码会使它们变成六个或十二个字节).

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).

您的代码中某处可能存在文本转码错误,而将ASCII子集中的所有内容转义都可以掩盖该问题. JSON规范要求所有数据都使用Unicode编码.

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和转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆