Home >Web Front-end >JS Tutorial >XML replacement----JSON_json

XML replacement----JSON_json

WBOY
WBOYOriginal
2016-05-16 19:11:161261browse

I personally think that PHP and Javascript each have their own strengths. If they can be perfectly combined, many miracles will be created! This is my first time writing such an article, bugs are inevitable, please correct me~
Nowadays, AJAX applications are very popular, and in AJAX, the communication between client-side Javascript and server-side dynamic script is the key. If the information transmitted is relatively simple, we usually use strings directly. If the information structure is more complex, XML documents are usually used. Although XML documents are widely used, it is not easy to generate them with PHP and process them with Javascript. Here I recommend a better simplified alternative to XML documents: JSON!
First let’s understand the basics of Javascript.
1. To create an array, you can use JS’s built-in class Array to initialize it, or you can use the JSON symbol "[]". The essence of arr1 and arr2 created in the following two ways is the same:

var arr1 = new Array();
arr1[0] = "apple";
arr1[1] = "google";
arr1[2] = "longbill";

var arr2 = ["apple","google","longbill"];
Note that the index of the array can also be String, such as arr1["name"] = "longbill"; At this time, the array is equivalent to an object. . .
2. To create an object, you can use the JS built-in class Object to initialize it, or you can use the JSON symbol "{}". The essence of obj1 and ob2 created in the following two ways is also the same:


var obj1 = new Object();
obj1.name = "longbill";
obj1.age = 18;

var obj2 = { name:"longbill",age:18 };
Note that the space between "{" and "}" here must be written in the form of "key:value", and Different "key:value" should be separated by ",". "Key" can also contain special characters such as spaces. In this case, "" (quotation marks) must be used to quote, such as "phone number":123456
In fact, in JS, the essence of an array is an object, and the object itself is also an array. Therefore, obj1.name and obj1["name"] are the same reference.
We can also define complex S objects by nesting JSON symbols:


var people = [
{
name: "longbill",
age: 18
},
{
name:"neal",
age:19
},
{
name:"glocklee",
Age:17
}
];
//You should understand this~~
Most XML documents can be expressed in JSON:





Ju love
$15


Javascrip
$25



If you use JSON, it can be expressed as:

{
root:
[
{
name:"Ju love",
price:"$15"
},
{
name:"Javascript",
price:" $25"
}
]}

Is it a lot simpler?

And the client-side JS is also very simple to process. Just use the string function "eval" to extract the JSON information. If it is an XML document, you don't need a lot of standard DOM operations. Extract the data from it. For example:


//A JSON file (string) has been downloaded from the server using AJAX and stored in the variable json
eval("var myvar = " json);
//In this way, the information in JSON is expressed in the variable myvar.
Disadvantages: Once the JSON format is incorrect, it will cause server-side JS system errors or even crashes.
Solution:
It is best to use try (test execution) before eval, such as


//A JSON file (string) has been downloaded from the server using AJAX, save it In the variable json
try {
eval("var myvar = " json);
} catch(e) { alert('json syntax error!'); }
//In this way, even if If the JSON format is incorrect, only a prompt box will pop up instead of a script error!

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