Why is JSON.parse not working?

Why is JSON.parse not working properly? In this example, the warning does not fire:

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Testing JSON.parse</title> <script type="text/javascript" src="js/json2.js"> // json2.js can be found here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js </script> <script type="text/javascript"> function testJSONParse() { var text = '[{"a":"w","b","x"},{"a":"y","b":"z"}]'; alert(JSON.parse(text)); } window.onload = testJSONParse; </script> </head> <body> </body> </html> 

In firefox, the error console says "JSON.parse". Not very descriptive.

This is a simplification of the problem in which I use AJAX to retrieve data from the database and get the result as a JSON string (a string representing a JSON object) of the same form as text in the above example.

+4
source share
2 answers

Your JSON is not formatted correctly:

 var text = '[{"a":"w","b","x"},{"a":"y","b":"z"}]'; ^-- This should be a ':' 

It should be:

 var text = '[{"a":"w","b":"x"},{"a":"y","b":"z"}]'; 
+10
source

input error

var text = '[{"a": "w", "b" : "x"}, {"a": "y", "b": "z"}]';

 //below is correct one var text = '[{"a":"w","b":"x"},{"a":"y","b":"z"}]'; alert(JSON.parse(text)); 
+2
source

Source: https://habr.com/ru/post/1342386/


All Articles