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:
- When parsing background JSON data with Ajax: unexpected token o in JSON at position 1
- Unexpected token ‘ in JSON at position 2 at JSON.parse
- Uncaught SyntaxError: Unexpected token o
- [Solved] Error-Javascript: Uncaught Error: Malformed UTF-8 data at Object.stringify (crypto-js.js:478) at W…
- How to Solve Error Unexpected token u in JSON at position 0
- Postman passing Josn field error: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token
- [Solved] json.decoder.JSONDecodeError: Expecting ‘,‘ delimiter: line xx column xx (char xxx)
- [Solved] TypeError: string indices must be integers, not str
- [Solved] Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse