javascript converts an JSON object to an JSON string

  • 2020-07-21 06:56:22
  • OfStack

This article illustrates how to convert JSON objects to JSON strings in javascript. Share to everybody for everybody reference. The details are as follows:


<script type="text/javascript">
//  According to the JSON The name of the property of the object gets the value of the property 
var jsonObj = { name: "jxqlovejava" }; // JSON object 
console.log(jsonObj.name); // "jxqlovejava"
var jsonStr = '{ name: "jxqlovejava" }';
// JSON String to a JSON Object methods 1
var jsonObj2 = eval("(" + jsonStr + ")");
console.log(jsonObj2.name); // jxqlovejava
// JSON String to a JSON Object methods 2
var jsonObj3 = JSON.parse(jsonStr);
console.log(jsonObj3.name); // jxqlovejava
// JSON Object to JSON string 
String jsonStr2 = JSON.stringify(jsonObj);
console.log(jsonStr2); // { name: "jxqlovejava" }
</script>

Hopefully, this article has been helpful in your javascript programming.


Related articles: