Parsing JSON (JavaScript Object Notation) data in JavaScript involves converting a JSON string into a JavaScript object. Consider the following JSON response:
var response = '{ "result": true, "count": 1 }';
To access the result and count values from this JSON string, you can use the JSON.parse() method:
const response = '{ "result": true, "count": 1 }'; const jsonObject = JSON.parse(response); console.log(jsonObject.result); // true console.log(jsonObject.count); // 1
The JSON.parse() method converts the JSON string response into a JavaScript object named jsonObject. This object can then be used to access the individual properties jsonObject.result and jsonObject.count as needed.
Example:
const json = '{ "fruit": "pineapple", "fingers": 10 }'; const obj = JSON.parse(json); console.log(obj.fruit, obj.fingers); // pineapple, 10
Note: The JSON.parse() method is supported in all major browsers and Node.js. It is a convenient and efficient way to parse JSON data and extract its values into JavaScript objects.
The above is the detailed content of How to Parse JSON Data in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!