Home  >  Article  >  Web Front-end  >  How to convert javascript object to json string

How to convert javascript object to json string

青灯夜游
青灯夜游Original
2021-04-08 15:12:599187browse

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]])

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"}

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!

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