简体   繁体   English

为什么 Javascript 不能从字符串文字中解析这个 JSON 数组?

[英]Why can't Javascript parse this JSON array from a string literal?

What I am trying to do is simple.我想要做的很简单。 Parse this array holding json objects into a Javascript array.将此包含 json 对象的数组解析为 Javascript 数组。

var merchantsJson = JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\u0022\u003C/div\u003E"}]');

But the unicode character \< seems to be breaking the parser.但是 unicode 字符 \< 似乎破坏了解析器。 In the chrome console I see "Uncaught SyntaxError: Unexpected token <"在 Chrome 控制台中,我看到“Uncaught SyntaxError: Unexpected token <”

A little more info.多一点信息。 The above is what the code is evaluated to.以上是代码的评估结果。 In reality the code contains a jsp expression.实际上,代码包含一个jsp 表达式。

var merchantsJson = JSON.parse('${jsonArr}');

If I remove the single quotes, there is no issue, but eclipse give me an "missing semicolon" error message.如果我删除单引号,就没有问题,但是 eclipse 会给我一个“缺少分号”的错误消息。 Is it possible to parse the array with the quotes as I am trying to do?是否可以像我一样用引号解析数组?

The interpolation of ${jsonArr} is already a JavaScript object. ${jsonArr}的插值已经是一个 JavaScript 对象。 When you wrap it in '${jsonArr}' this turns it into a string and you have to use JSON.parse .当您将其包装在'${jsonArr}'这会将其转换为字符串,您必须使用JSON.parse

There's no need to make it a string.没有必要把它变成一个字符串。 You can just do var merchantsArray = ${jsonArr} .你可以只做var merchantsArray = ${jsonArr} JSON constructs are already interoperable with JavaScript code. JSON 结构已经可以与 JavaScript 代码互操作。

Because there's an extra " in your string literal that is encoded by " :因为您的字符串文字中有一个额外的""编码:

> '[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\u0022\u003C/div\u003E"}]'
[{"id":61693,"name":"Más"},{"id":61690,"name":"'"</div>"}]

In short, your JSON in the string is invalid.简而言之,您在字符串中的 JSON 无效。 You would need to escape the unicode escape sequences for the quotes in the string literal ( "'"</div>" ), by using您需要使用转义字符串文字 ( "'"</div>" ) 中引号的 unicode 转义序列

JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\\u0022\u003C/div\u003E"}]'
//                                                               ^

or escape the quote character ( "'\\"</div>" ):或转义引号字符( "'\\"</div>" ):

JSON.parse('[{"id":61693,"name":"Más"},{"id":61690,"name":"\u0027\\\u0022\u003C/div\u003E"}]');
//                                                               ^^

However, there actually is no need to use JSON at all.但是,实际上根本不需要使用 JSON。 Just output a JS array literal into your code:只需将 JS 数组文字输出到您的代码中:

var merchantsJson = ${jsonArr};

Try to replace \\u\u003c/code> with \\\\u\u003c/code> .尝试用\\\\u\u003c/code>替换\\u\u003c/code> \\\\u\u003c/code> 。 If you don't, JSON parser receives already decoded Unicode, which created polluted JSON.如果不这样做,JSON 解析器会接收已解码的 Unicode,这会创建污染的 JSON。

It's not because of \<, rather the " character is causing the issue, since it's a quotation mark and JavaScript treats it literally ending the string.这不是因为 \<,而是"字符导致了问题,因为它是一个引号,而 JavaScript 将它视为字面上的字符串结尾。

You need to escape that character: \\" .您需要转义该字符: \\"

you have to use special character in your JSON string, you can escape it using \\ character.您必须在 JSON 字符串中使用特殊字符,您可以使用 \\ 字符对其进行转义。

you need to replace \\ with \\\\ .你需要用\\\\替换\\

[{\"id\":61693,\"name\":\"Más\"},{\"id\":61690,\"name\":\"\\u0027\\u0022\\u003C/div\\u003E\"}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM