Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JS object to JSON string

If I defined an object in JS with:

var j={"name":"binchen"}; 

How can I convert the object to JSON? The output string should be:

'{"name":"binchen"}' 
like image 416
Bin Chen Avatar asked Nov 12 '10 08:11

Bin Chen


People also ask

How do I convert a JSON object to a string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

How do you pass a JSON object into a string in Java?

JSONObject json= (JSONObject) JSONValue. parse(jsonData); JSONObject data = (JSONObject) json. get("data"); After you have parsed the json data, you need to access the data object later get "map" data to json string.

How do I convert something to JSON?

String data can be easily converted to JSON using the stringify() function, and also it can be done using eval() , which accepts the JavaScript expression that you will learn about in this guide.

Which method converts a JSON string to a JavaScript object?

parse() JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.


1 Answers

All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:

var j = {    "name": "binchen"  };  console.log(JSON.stringify(j));
like image 196
Andris Avatar answered Sep 29 '22 11:09

Andris