Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 15:50:30
Original
1214 people have browsed it

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>

Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!