javascript tutorial - [Solved-5 Solutions] Convert JS object to JSON string - javascript - java script - javascript array
Problem:
How to Convert JS object to JSON string?
Solution 1:
defined an object in JS with:
var j={"name":"binchen"};
click below button to copy the code. By JavaScript tutorial team
Solution 2:
var j={"name":"binchen"};
JSON.stringify(j); // '{"name":"binchen"}'
click below button to copy the code. By JavaScript tutorial team
Solution 3:
With JSON.stringify()
found in json2.js or native in most modern browsers.
JSON.stringify(value, replacer, space)
value any JavaScript value, usually an object or array.
replacer an optional parameter that determines how object
values are stringified for objects. It can be a
function or an array of strings.
space an optional parameter that specifies the indentation
of nested structures. If it is omitted, the text will
be packed without extra whitespace. If it is a number,
it will specify the number of spaces to indent at each
level. If it is a string (such as '\t' or ' '),
it contains the characters used to indent at each level.
This method produces a JSON text from a JavaScript value.
click below button to copy the code. By JavaScript tutorial team
Solution 4:
son Stringify can convert our js object to json
var x = {"name" : "name1"};
JSON.stringify(x);
click below button to copy the code. By JavaScript tutorial team
Solution 5:
JSON.stringify() method to convert JSON object to String.
var j={"name":"binchen"};
JSON.stringify(j)