获取字符串的含义
我有以下字符串“\u3048\u3075\u3057\u3093”
。我得到了字符串 来自网页作为 JSONP 返回数据的一部分。
那是什么?它看起来像UTF8,但是它应该看起来像“U+3048U+3075U+3057U+3093”
吗?
反斜杠 (\
) 的含义是什么?
如何将其转换为人类可读的形式?
我正在寻找 Ruby 的解决方案,但是对这里发生的事情的任何解释都值得赞赏。
I have the following string "\u3048\u3075\u3057\u3093"
. I got the string
from a web page as part of returned data in JSONP.
What is that? It looks like UTF8, but then should it look like "U+3048U+3075U+3057U+3093"
?
What's the meaning of the backslashes (\
)?
How can I convert it to a human-readable form?
I'm looking to a solution with Ruby, but any explanation of what's going on here is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
U+3048
语法通常用于表示字符的 Unicode 代码点。此类代码点是固定的,不依赖于编码(UTF-8、UTF-32...)。JSON 字符串由除双引号、反斜杠和 U+0000 到 U+001F 范围内的字符(控制字符)之外的 Unicode 字符组成。字符可以用以
\u
开头并后跟代表字符的 Unicode 代码点的 4 个十六进制数字的转义序列来表示。这是 JavaScript 语法(JSON 是它的子集)。在 JavaScript 中,反斜杠用作转义字符。The
U+3048
syntax is normally used to represent the Unicode code point of a character. Such code point is fixed and does not depend on the encoding (UTF-8, UTF-32...).A JSON string is composed of Unicode characters except double quote, backslash and those in the U+0000 to U+001F range (control characters). Characters can be represented with a escape sequence starting with
\u
and followed by 4 hexadecimal digits that represent the Unicode code point of the character. This is the JavaScript syntax (JSON is a subset of it). In JavaScript, the backslash is used as escape char.它是 Unicode,但不是 UTF-8,而是 UTF-16。您可能会忽略代理项对并将其视为 Unicode 代码字符的 4 位十六进制代码点。
使用 Ruby 1.9:
打印:
It is Unicode, but not in UTF-8, it is in UTF-16. You might ignore surrogate pairs and deem it as 4-digit hexadecimal code points of a Unicode code character.
Using Ruby 1.9:
Prints:
JSON 中的 Unicode 字符被转义为反斜杠 u 后跟四个十六进制数字。请参阅 json.org 上的字符串生成方式。
任何 JSON 解析器都会将其转换为适合您平台的正确表示形式(如果没有,那么根据定义,它不是 JSON 解析器)
Unicode characters in JSON are escaped as backslash u followed by four hex digits. See the string production on json.org.
Any JSON parser will convert it to the correct representation for your platform (if it doesn't, then by definition it is not a JSON parser)