Home >Web Front-end >JS Tutorial >How to convert json data to array in javascript

How to convert json data to array in javascript

青灯夜游
青灯夜游Original
2021-10-13 14:59:4124308browse

In javascript, you can use the "JSON.parse()" method to convert json data into an array. This method can convert a JSON string into a JavaScript value or object. The syntax "JSON.parse(json data)".

How to convert json data to array in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JSON is a format used to store and transmit data. JSON is usually used by the server to transfer data to web pages.

The JSON format is syntactically identical to the code that creates JavaScript objects. Because they are similar, JavaScript programs can easily convert JSON data into JavaScript objects.

javascript json data to array

//json字符串转数组
var jsonStr = '[1,2,3,{"a":1}]';
var jsarr=JSON.parse( jsonStr );
console.log(jsarr);
console.log(jsarr[0]);

How to convert json data to array in javascript

Description:

JSON.parse() method is used Convert a JSON string to a JavaScript value or object. Syntax format:

JSON.parse(text[,reviver])

Parameter description:

  • text: required, a valid JSON string.

  • reviver: Optional, a function that converts the result. This function will be called for each member of the object.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert json data to array 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