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

How to convert json object to string in javascript

青灯夜游
Release: 2023-01-03 09:31:52
Original
19229 people have browsed it

In JavaScript, you can use the "JSON.stringify()" method to convert a json object into a string, and then return a string containing JSON text; the syntax format is "JSON.stringify(value)".

How to convert json object to string in javascript

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

Convert json object to string in javascript

Suppose we have the following object:

var jsonObj = {
    "CityId":"18",
    "CityName":"西安2"
};
Copy after login

We want to convert json object to string , you can use the JSON.stringify() method. This method does not support older versions of IE browsers, such as: ie8 (compatibility mode), ie7, and ie6.

var jsonStr = JSON.stringify(jsonObj);
console.log(jsonStr);
Copy after login

Output result:

How to convert json object to string in javascript

[Recommended learning: js basic tutorial]

Instructions:

The JSON.stringify() method is used to convert JavaScript values ​​to JSON strings.

Syntax

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

Parameter description:

  • value:

    Required, the JavaScript value to be converted (usually an object or array ).

  • replacer:

    Optional. The function or array used to convert the result.

    If replacer is a function, JSON.stringify will call the function, passing in the key and value of each member. Use the return value instead of the original value. If this function returns undefined, the member is excluded. The key of the root object is an empty string: "".

    If replacer is an array, only members with key values ​​in the array are converted. Members are converted in the same order as the keys in the array.

  • #space:

    Optional, the text adds indentation, spaces and newlines. If space is a number, the return value text is indented at each level specified number of spaces. If space is greater than 10, the text is indented by 10 spaces. Space can also use non-digits, such as: \t.

Return value: Returns a string containing JSON text.

For more programming related knowledge, please visit: Programming Video! !

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