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

Example of JavaScript method for parsing JSON format data

高洛峰
Release: 2017-02-17 16:59:21
Original
1636 people have browsed it

The example in this article describes the method of JavaScript parsing JSON format data. Share it with everyone for your reference, as follows:

1. Use the eval() function provided by JavaScript

function JsonText1() {
  var strJSON = "{'Name':'Kevin','Age':'23'}"; //得到的JSON
  var obj = eval("(" + strJSON + ")"); //转换后的JSON对象
  alert(obj.Name);
}
Copy after login

2. Use JSON object

① Use stringify of JSON object () function, convert the object into JSON

Syntax: var str = JSON.stringify(data);

② Use the parse() function of the JSON object to convert JSON into the object

Syntax: var data = JSON.parse(str);

var jsonStr = "";
//使用JSON对象的stringify()函数,将对象转换成JSON
function JsonText2() {
  var data = new Object;
  data.Name = "Kevin";
  data.Age = 23;
  jsonStr = JSON.stringify(data);
  alert(jsonStr);
}
//使用JSON对象的parse()函数,将JSON转换成对象
function JsonText3() {
  var data = JSON.parse(jsonStr);
  alert(data.Name);
}
Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

For more examples of JavaScript methods to parse JSON format data and related articles, please pay attention to the PHP Chinese website!

Related labels:
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