简体   繁体   English

JSON.parse得到“Uncaught SyntaxError:Unexpected token h”

[英]JSON.parse get “Uncaught SyntaxError: Unexpected token h”

I get the syntax error when I try to pass the following string: 我尝试传递以下字符串时出现语法错误:

JSON.parse("[{\"Date\": \"4/4/2016 4:15:19 PM\", \"Message\":\"<h3>New 
Message</h3> Generated at 4/4/2016 4:15:19 PM.<br/><br/>Heavy Responsive 
URL: <a href=\"https://performingarts.withgoogle.com/en_us\" ></a><br/><br/>
<img src=\"https://s-media-cache-ak0.pinimg.com/236x/06/bd/ac/06bdacc904c12abdce3381ba1404fd7e.jpg\" /> \"} ]");

I know that the error come from the link when I use double quote. 我知道当我使用双引号时,错误来自链接。

If I use single quote then no issue, but the data is getting from server side, I got no control over what going to pass in so I can only control on my side. 如果我使用单引号然后没有问题,但数据是从服务器端获得的,我无法控制将要传递的内容,因此我只能控制在我身边。

From what I read from the internet so far, I tried the following: 从我到目前为止从互联网上读到的内容,我尝试了以下内容:

  1. Use JSON.stringify first, then only use JSON.parse . 首先使用JSON.stringify ,然后只使用JSON.parse I can parse with no issue but problem occur when I try to loop the data. 我可以解决没有问题,但当我尝试循环数据时出现问题。 Instead of looping it as JSON, the loop take the data as string and loop every single text. 循环将数据作为字符串并循环每个单独的文本,而不是将其循环为JSON。

  2. Escape every double quote which I'm currently doing, but it's not working as shown above. 逃避我目前正在做的每一个双重报价,但它没有如上所示工作。 But if I replace every double quote to literal, I'm afraid some of the message that suppose to be double quote will turn into literal as well, which will result in weird looking message. 但是,如果我将每个双引号替换为字面值,我担心一些假设为双引号的消息也将转换为字面值,这将导致奇怪的消息。

Please advice what other alternative I have to solve this. 请建议我有什么其他选择来解决这个问题。

You have JSON embedded in a JavaScript string literal. 您在JavaScript字符串文字中嵌入了JSON。

" and \\ are special characters in JSON and are also special characters in a JavaScript string literal. "\\是JSON中的特殊字符,也是JavaScript字符串文字中的特殊字符。

href=\\"https: escapes the " in the JavaScript string literal. href=\\"https:在JavaScript字符串文字中转义" It then becomes a " in the JSON. That causes an error. 然后它变成了"在JSON中。这会导致错误。

When you want the " as data in the JSON you must: 当您想要"作为JSON中的数据时,您必须:

  • Escape the " for JavaScript (as you are doing already) 逃避"为JavaScript(你已经在做)
  • Escape the " for JSON by adding a \\ . 通过添加\\逃避" for JSON "
  • Escape the \\ for JavaScript 逃避\\为JavaScript

href=\\\\\\"https:

暂无
暂无

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

相关问题 未捕获到的SyntaxError:意外令牌[与JSON.parse - Uncaught SyntaxError: Unexpected token [ with JSON.parse "未捕获的 SyntaxError:带有 JSON.parse 的意外标记" - Uncaught SyntaxError: Unexpected token with JSON.parse 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) Javascript JSON.parse:Uncaught SyntaxError:意外的令牌N. - Javascript JSON.parse: Uncaught SyntaxError: Unexpected token N 未捕获的语法错误:意外的令牌 c JSON.parse - Uncaught SyntaxError: Unexpected token c JSON.parse Uncaught SyntaxError:意外的令牌o- JSON.Parse - Uncaught SyntaxError: Unexpected token o- JSON.Parse JSON.parse()和$ .parseJSON()中的“未捕获的SyntaxError:意外的令牌?” - “Uncaught SyntaxError: Unexpected token ?” in both JSON.parse() and $.parseJSON()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM