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

A concise introduction to json_json

WBOY
Release: 2016-05-16 19:00:29
Original
1127 people have browsed it

In JS we can directly define an object:

Copy code The code is as follows:

var obj={name:"Blue",age:10};

The above is actually the so-called JSON. I will quote other people’s more complicated examples:
Copy code The code is as follows:

{ firstName: "Brett", lastName: "McLaughlin", email: "brett@ jb51.net" }

Copy code The code is as follows:

{ people: [
{ firstName: "Brett", lastName: "McLaughlin", email: "brett@jb51.net" },
{ firstName: "Jason", lastName: "Hunter", email: "jb51 .net" },
{ firstName: "Elliotte", lastName: "Harold", emai": "elharo@jb51.net" }
]}
{ people1: [
{ firstName: "Brett", lastName: "McLaughlin", email: "brett@jb51.net" },
{ firstName: "Jason", lastName: "Hunter", email: "jb51.net" },
{ firstName: "Elliotte", lastName: "Harold", emai": "elharo@jb51.net" }
],
people2: [
{ firstName: "Brett", lastName: "McLaughlin", email: "brett@jb51.net" },
{ firstName: "Jason", lastName:"Hunter", email: "jb51.net" },
{ firstName: "Elliotte", lastName:"Harold ", emai": "elharo@jb51.net" }
],
people3: [
{ firstName: "Brett", lastName: "McLaughlin", email: "brett@jb51.net" } ,
{ firstName: "Jason", lastName: "Hunter", email: "jb51.net" },
{ firstName: "Elliotte", lastName: "Harold", emai": "elharo@jb51. net" }
]
}

2. Format the text transmitted in the background that meets JSON conditions.
Because the biggest use of JSON is AJAX applications, the key is how to convert the text into a javascript object. Assuming that the text we request is data, then we parse it as follows:
Copy code The code is as follows:

data=data.replace(/n | r/g,"");
var obj=eval( '(' data ')');

Because spaces and carriage returns will also be output when we output in the background, so we need to replace them with regular expressions here, and then use eval to convert them into objects. Called.
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!