How to convert javascript object to json string

青灯夜游
Release: 2023-01-04 09:35:15
Original
9159 people have browsed it

In javascript, you can use the built-in method "JSON.stringify()" to convert js objects into JSON strings; this method can convert JavaScript objects or arrays into JSON strings, and the syntax format is "JSON .stringify(object)".

How to convert javascript object to json string

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

Convert javascript object to json string

Use JavaScript built-in methodJSON.stringify()Convert JavaScript object to JSON string

Syntax format:

JSON.stringify(value[, replacer[, space]])
Copy after login

The JSON.stringify() method converts a JavaScript object or value into a JSON string. If the replacer function is specified, you can choose to replace the value. If the replacer function is specified, array, you can include only specified properties.

Parameter description:

How to convert javascript object to json stringExample:

// 对象 var dog = { name: "gougou", "age": "2" }; // JSON字符串 var newString = JSON.stringify(dog); console.log(typeof newString); // 输出:string console.log(newString); // 输出:{"name":"gougou","age":"2"}
Copy after login

Output:

How to convert javascript object to json string

Recommended learning:javascript advanced tutorial

The above is the detailed content of How to convert javascript object to json string. 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 admin@php.cn
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!