Home  >  Article  >  Web Front-end  >  Examples of conversion between JSON objects and JSON strings in javascript_javascript skills

Examples of conversion between JSON objects and JSON strings in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:50:301233browse

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn