Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript JSON.stringify()

JavaScript JSON.stringify()

JavaScript JSON JavaScript JSON


The JSON.stringify() method is used to convert a JavaScript value into a JSON string.

Syntax


JSON.stringify(value[, replacer[, space]])

Parameter Description:

Return Value:

Returns a string containing the JSON text.

Example

Example

var

str

=

{

"

name

"

:

"

Runoob Tutorial

"

,

"

site

"

:

"

http://www.runoob.com

"

}

str_pretty1

=

JSON

.

stringify

(

str

)

document

.

write

(

"

With only one parameter:

"

)

;

document

.

write

(

"


"

)

;

document

.

write

(

"

"

 + 

str_pretty1

 + 

"

"

)

;

document

.

write

(

"


"

)

;

str_pretty2

=

JSON

.

stringify

(

str

,

null

,

4

)

//

Using four spaces for indentation

document

.

write

(

"

Using the parameter:

"

)

;

document

.

write

(

"


"

)

;

document

.

write

(

"

"

 + 

str_pretty2

 + 

"

"

)

;

//

pre is used to format the output

Try it »

JavaScript JSON JavaScript JSON