Home > Web Front-end > JS Tutorial > body text

How to convert array to json format in javascript

青灯夜游
Release: 2023-01-05 16:09:36
Original
28070 people have browsed it

In javascript, you can use the conversion function "JSON.stringify()" to convert the array into json format. The syntax format is "JSON.stringify(array)"; this function can convert arrays or objects. Then return a string containing the JSON text.

How to convert array to json format in javascript

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

javascript array to JSON format

Point 1: Conversion function JSON.stringify()

Point 2: When writing an array in js var data = new Array() But if you want to convert to json for display, you must write var data = {}, otherwise the converted json will be empty.

First look at an incorrect format:

Copy after login

Effect:

## Correct format:

Copy after login
Result:


Summary: It can be seen that what we define is actually an object format. var data = {} is the abbreviation of var data = new Object(). JSON.stringify() cannot convert an array with custom subscripts. It can convert an array with 0 1 2 subscripts. Of course, that may not be what you want.

For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of How to convert array to json format in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!