简体   繁体   中英

Why I can't parse JSON in JavaScript?

JSON contains one object:

results[0] = { 'MAX(id)': 1 }

And this code doesn't work:

var text = results[0];
var obj = JSON.parse(text);
console.log(obj.MAX(id));

results[0] is already an object type

You can parse only from string to object like this:

JSON.parse('{ "MAX(id)": 1 }');

Your object is already a JSON. You don't need to parse it. To access MAX(id) property, you can use [] notation as follows:

results[0] = { 'MAX(id)': 1 };
console.log(results[0]['MAX(id)']);

Your result[0] is a real javascript object. JSON.parse transforms text into objects, so you can't parse other objects with it.

  var results = { 'MAX(id)': 1 }; //var text = results; //var obj = JSON.parse(text); alert(results['MAX(id)']); 

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