Home >Web Front-end >JS Tutorial >How to Parse JSON Data in JavaScript?

How to Parse JSON Data in JavaScript?

Barbara Streisand
Barbara StreisandOriginal
2024-12-22 05:35:11828browse

How to Parse JSON Data in JavaScript?

How to Parse JSON in JavaScript?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn