简体   繁体   中英

Uncaught SyntaxError: Unexpected token c JSON.parse

I'm getting an uncaught Syntax Error when I try to parse a JSON string , I'm not sure why it is failing. I've tested the JSON string and it is valid.

Can someone please help me with this issue.

My code snippet (in chrome) is as follows:

var input = 'c:out escapeXml="false" value="[{"registeredCustomers":"81","node":"RAGS"},{"registeredCustomers":"39","node":"SBSA"}]" />';
var arr1 = [];
obj1 = JSON.parse(input);

You have to do

input = input.replace("c:out escapeXml='false' value='", "")

and then JSON.parse(input);

Json.parse expect the string. You can try this:

   var input = '[{\"registeredCustomers":"81","node":"RAGS"},      
   "registeredCustomers":"39","node":"SBSA"\}]';
   var arr1 = [];
   obj1 = JSON.parse(input);
   console.log(obj1);

it will return the following output:

[Object, Object, $family: function, $constructor: function, each: function, clone: function, clean: function…]
0: Object
node: "RAGS"
registeredCustomers: "81"
__proto__: Object
1: Object
node: "SBSA"
registeredCustomers: "39"
__proto__: Object
length: 2
__proto__: Array[0]

You are getting that error Unexpected token c JSON.parse due to the multiple time usage of quote in the input. And the error message is pointing to starting of the line.

value="[{"registeredCustomers":"81","node":"RAGS"},{"registeredCustomers":"39","node":"SBSA"}]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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