Tag Archives: Unexpected token o in JSON at position 1 at JSON.parse () SyntaxError: Unexp…

Unexpected token o in JSON at position 1 at JSON.parse () SyntaxError: Unexp…

This problem was encountered once before when doing a project. At that time, according to the practice on the Internet, after removing the layer of JSON. Parse (), there was no such error report, and the data could be used normally. I didn’t think much about it, and I didn’t go deep into the reason. But this time it happened again, so this time I have to find out why

Let’s see what it does first.
JSON. Parse() is used to parse a JSON object from a string, such as

var str = '{"name":"LeonWu","age":"18"}'
JSON.parse(str);
//The result is an object
// age: "18"
// name: "LeonWu";
JSON. Stringify() is used to parse strings from an object, such as
var a = {a:1,b:2,c:"LeonWu"};
JSON.stringify(a);
//the result is "{" a ": 1," B ": 2," C ":" leonwu "}"

 

the reason for reporting an error:
because the data you want to convert is originally an object, this method parses a string into a JSON object, and you will report an error if you convert it again

Why there is such an error:
because when an object is passed as a parameter to JSON. Parse(), it will convert the object to string by default,
it will call the tostring() method on the prototype first; The result is “[object object]”, JSON. Parse interprets the first character ‘[‘as the beginning of the array, and the second character’ o ‘doesn’t know how to deal with it; So the above error message unexpected token o in JSON at position 1 is thrown
———————

JSON string:
var STR1 = ‘{“name”: “CXH”, “sex”: “man”}’
JSON object:
var STR2 = {“name”: “CXH”, “sex”: “man”}

First, convert JSON string to JSON object

To use STR1 above, you must first convert it to a JSON object using the following method:

//From JSON string to JSON object

var obj = eval(‘(‘ + str + ‘)’);

or

var obj = str.parseJSON(); // From JSON string to JSON object

or

var obj = JSON.parse(str); // From JSON string to JSON object

Then, you can read as follows:

Alert(obj.name);

Alert(obj.sex);

Special note: if obj is originally a JSON object, it will still be a JSON object after using eval() function (even multiple conversions), but there will be problems after using parsejson() function (syntax exception is thrown)

Second, you can use tojsonstring() or the global method JSON. Stringify() to convert a JSON object to a JSON string

for example:

var last=obj.toJSONString(); // Converting JSON objects to JSON characters

or

var last=JSON.stringify(obj); // Converting JSON objects to JSON characters

when using json.parse, the returned data must be in strict JSON format, and the key value must be wrapped in double quotation marks, otherwise the browser will throw an exception
in addition, for non-standard JSON strings, such as the first example, although using Eval () can parse normally, from a security point of view, we must be careful to use Eval method. Standard JSON format