为什么JSON。用node . js中的编码字符解析扼流圈?

时间:2021-12-05 23:50:44

I'm attempting to look up the word "flower" in Google's dictionary semi-api. Source:

我想在谷歌的字典半api中查找“flower”这个词。来源:

https://gist.github.com/DelvarWorld/0a83a42abbc1297a6687

https://gist.github.com/DelvarWorld/0a83a42abbc1297a6687

Long story short, I'm calling JSONP with a callback paramater then regexing it out.

长话短说,我用一个回调参数调用JSONP,然后重新调用它。

But it hits this snag:

但它遇到了这个障碍:

undefined:1
ple","terms":[{"type":"text","text":"I stopped to buy Bridget some \x3cem\x3ef
                                                                    ^
SyntaxError: Unexpected token x
    at Object.parse (native)

Google is serving me escaped HTML characters, which is fine, but JSON.parse cannot handle them?? What's weirding me out is this works just fine:

谷歌为我提供转义的HTML字符,这很好,但是JSON。解析不能处理他们? ?让我感到奇怪的是这招很管用:

$ node

> JSON.parse( '{"a":"\x3cem"}' )
  { a: '<em' }

I don't get why my thingle is crashing

我不明白为什么我的东西要碎了

Edit These are all nice informational repsonses, but none of them help me get rid of the stacktrace.

编辑这些都是很好的信息反馈,但是没有一个能帮助我摆脱堆栈跟踪。

4 个解决方案

#1


1  

\xHH is not part of JSON, but is part of JavaScript. It is equivalent to \u00HH. Since the built-in JSON doesn't seem to support it and I doubt you'd want to go through the trouble of modifying a non-built-in JSON implementation, you might just want to run the code in a sandbox and collect the resulting object.

\xHH不是JSON的一部分,而是JavaScript的一部分。相当于\u00HH。由于内置的JSON似乎不支持它,而且我怀疑您是否愿意麻烦地修改非内置的JSON实现,您可能只想在沙箱中运行代码并收集结果对象。

#2


0  

According to http://json.org, a string character in a JSON representation of string may be:

根据http://json.org,字符串的JSON表示中的字符串字符可能是:

  • any-Unicode-character- except-"-or--or- control-character
  • any-Unicode-character——除了“或者——或者——控制字符
  • \"
  • \”
  • \
  • \
  • \/
  • \ /
  • \b
  • \ b
  • \f
  • \ f
  • \n
  • \ n
  • \r
  • r \
  • \t
  • \ t
  • \u four-hex-digits
  • \ u four-hex-digits

So according to that list, the "json" you are getting is malformed at \x3

根据这个列表,你得到的“json”在\x3上是畸形的

#3


0  

The reason why it works is because these two are equivalent.

它起作用的原因是这两个是等价的。

JSON.parse( '{"a":"\x3cem"}' )

and

JSON.parse( '{"a":"<em"}' )

you string is passed to JSON.parse already decoded since its a literal \x3cem is actually <em

将字符串传递给JSON。解析已经解码,因为它是一个文本\x3cem实际上是

Now, \xxx is valid in JavaScript but not in JSON, according to http://json.org/ the only characters you can have after a \ are "\/bfnrtu.

现在,根据http://json.org/, \xxx在JavaScript中是有效的,但在JSON中无效。

#4


0  

answer is correct, but needs couple of modifications. you might wanna try this one: https://gist.github.com/Selmanh/6973863

答案是正确的,但是需要一些修改。您可能想试试这个:https://gist.github.com/Selmanh/6973863

#1


1  

\xHH is not part of JSON, but is part of JavaScript. It is equivalent to \u00HH. Since the built-in JSON doesn't seem to support it and I doubt you'd want to go through the trouble of modifying a non-built-in JSON implementation, you might just want to run the code in a sandbox and collect the resulting object.

\xHH不是JSON的一部分,而是JavaScript的一部分。相当于\u00HH。由于内置的JSON似乎不支持它,而且我怀疑您是否愿意麻烦地修改非内置的JSON实现,您可能只想在沙箱中运行代码并收集结果对象。

#2


0  

According to http://json.org, a string character in a JSON representation of string may be:

根据http://json.org,字符串的JSON表示中的字符串字符可能是:

  • any-Unicode-character- except-"-or--or- control-character
  • any-Unicode-character——除了“或者——或者——控制字符
  • \"
  • \”
  • \
  • \
  • \/
  • \ /
  • \b
  • \ b
  • \f
  • \ f
  • \n
  • \ n
  • \r
  • r \
  • \t
  • \ t
  • \u four-hex-digits
  • \ u four-hex-digits

So according to that list, the "json" you are getting is malformed at \x3

根据这个列表,你得到的“json”在\x3上是畸形的

#3


0  

The reason why it works is because these two are equivalent.

它起作用的原因是这两个是等价的。

JSON.parse( '{"a":"\x3cem"}' )

and

JSON.parse( '{"a":"<em"}' )

you string is passed to JSON.parse already decoded since its a literal \x3cem is actually <em

将字符串传递给JSON。解析已经解码,因为它是一个文本\x3cem实际上是

Now, \xxx is valid in JavaScript but not in JSON, according to http://json.org/ the only characters you can have after a \ are "\/bfnrtu.

现在,根据http://json.org/, \xxx在JavaScript中是有效的,但在JSON中无效。

#4


0  

answer is correct, but needs couple of modifications. you might wanna try this one: https://gist.github.com/Selmanh/6973863

答案是正确的,但是需要一些修改。您可能想试试这个:https://gist.github.com/Selmanh/6973863