简体   繁体   English

将字符串转换为JSON对象会引发意外令牌

[英]convert string to JSON object throws Unexpected token

i am trying to convert a string with escape characters to JSON but get error 我正在尝试将带有转义字符的字符串转换为JSON但出现错误

  var str = '[{"name":"content","readonly":false,"value":"<div class=\"blueheading\"><h2>Free Gifts for Him</h2><h3><a href=\"http://abcd.com/free-gifts-with-purchase-for-him.aspx\">View all for Him</a></h3></div>"},{"name":"cached","readonly":true,"value":true}]';
            var myObj = JSON.parse(str);

If you want to have \\" in a string literal, you have to escape the \\ : '\\\\"' . 如果要在字符串文字中使用\\" ,则必须转义\\'\\\\"'

This gives : 这给出了:

var str = '[{"name":"content","readonly":false,"value":"<div class=\\"blueheading\\"><h2>Free Gifts for Him</h2><h3><a href=\\"http://abcd.com/free-gifts-with-purchase-for-him.aspx\\">View all for Him</a></h3></div>"},{"name":"cached","readonly":true,"value":true}]';
var myObj = JSON.parse(str);

Read more about special characters in string literals 阅读有关字符串文字中特殊字符的更多信息

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

相关问题 使用JSON.parse将字符串(变量)转换为对象,错误意外令牌 - Convert a string (variable ) to object with JSON.parse , error unexpected token 在JavaScript中解析字符串json会引发意外令牌 - Parsing string json in javascript throws Unexpected token JSON字符串上的JSON解析抛出“无法将对象转换为原始值” - JSON parse on a JSON string throws “Cannot convert object to primitive value” 检索JSON引发错误:意外令牌: - Retrieving JSON throws error: Unexpected token : 从asyncStorage获取时的JSON.parse(object)引发错误:位置1的JSON中的意外令牌o - JSON.parse(object) when getting from asyncStorage throws error: Unexpected token o in JSON at position 1 从字符串解析对象时出现意外令牌和JSON - Unexpected token & in JSON when parsing object from string 读取 [对象:null 原型] 对象抛出 `SyntaxError: Unexpected token u in JSON at position 0` 错误 - Reading [Object: null prototype] object throws `SyntaxError: Unexpected token u in JSON at position 0` error 使用Javascript将字符串转换为对象(错误:JSON中位置1处的意外令牌t) - Converting string to object with Javascript (Error: Unexpected token t in JSON at position 1) API object 转为字符串 - JSON 中的意外令牌 o - API object turns to string - Unexpected token o in JSON 具有对象参数的React函数抛出意外令牌 - React function with object params throws an unexpected token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM