Home > Article > Web Front-end > Examples of conversion between JSON objects and JSON strings in javascript_javascript skills
The example in this article describes the implementation method of converting JSON objects and JSON strings in JavaScript. Share it with everyone for your reference. The details are as follows:
<script type="text/javascript"> // 根据JSON对象的属性的名称获取属性的值 var jsonObj = { name: "jxqlovejava" }; // JSON对象 console.log(jsonObj.name); // "jxqlovejava" var jsonStr = '{ name: "jxqlovejava" }'; // JSON字符串到JSON对象方法一 var jsonObj2 = eval("(" + jsonStr + ")"); console.log(jsonObj2.name); // jxqlovejava // JSON字符串到JSON对象方法二 var jsonObj3 = JSON.parse(jsonStr); console.log(jsonObj3.name); // jxqlovejava // JSON对象到JSON字符串 String jsonStr2 = JSON.stringify(jsonObj); console.log(jsonStr2); // { name: "jxqlovejava" } </script>
I hope this article will be helpful to everyone’s JavaScript programming design.