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)".
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:
Example:
// 对象 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:
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!