How to Solve Error: Unexpected token o in JSON at position 1

Unexpected token o in JSON at position 1

1、 Summary

In a word:

solution: remove the layer transformation of JSON. Parse()
error reason: because the data you want to convert is object, the JSON. Parse() method parses a string into a JSON object, and you will report an error if you convert it again

1. Cause of error: question in data of Vue_ list_ box:JSON.parse (‘{!! json_ encode($question_ list_ box !!}’), How to solve it

can use JSON in PHP_ After encode(), it is directly given to the window object of the page, which will be directly converted into a JSON object. In this way, you don’t need to use the JSON. Parse() function

2. What is the function of JSON. Parse()

JSON. Parse() is used to parse a JSON object from a string

2、 Unexpected token o in JSON at position 1

Refer to: unexpected token o in JSON at position 1 https://blog.csdn.net/wxl1555/article/details/79184076

 

Words written in the front

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 we met again, so we have to find out the reason this time

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);

// age: "18";
// name: "LeonWu";

JSON. Stringify () is used to parse a string from an object, such as

var a = {a:1,b:2,c:"LeonWu"};
 
 JSON.stringify(a);
 
 //output "{"a":1,"b":2,"c":"LeonWu"}"
 

The reason behind it

reasons for error reporting

Because the data you want to convert is originally object, JSON. Parse() is to parse a string into a JSON object, and you will report an error if you convert it again

Why is there such a mistake

When the object is passed as a parameter to JSON. Parse(), it will be converted to string by using the tostring() method, and the result is “[object]”

JSON. Parse() interprets the first character ‘[‘ as the beginning of the array, and the second character ‘o’ doesn’t know how to handle it; So the above error message unexpected token o in JSON at position 1 is thrown

 

The solution is to remove the layer transformation of JSON. Parse (), because the data you need to transform is a JSON object, so you don’t need to transform it

Similar Posts: