What is JSON
JSON is a lightweight data format based on a subset of JavaScript syntax, that is, array and object representation. Because JavaScript syntax is used, JSON definitions can be included in JavaScript files, and access to them does not need to be parsed in XML-based languages. However, before using JSON, it is very important to understand the special Syntax of arrays and object literally in Javascript.
1.1 array literal
Array literal, which is a set of JavaScript values separated by commas (,) enclosed by square brackets. For example:
VaR as = ["hi", 10, true, null];
1.2 object literal
The object literal is defined by two curly braces. You can place any number of "name-value" pairs in curly brackets to define the lattice string value ". Except the last line, each "name-value" pair must be followed by a comma (which is similar to the definition of the Union array in Perl ). For example:
VaR ocar = {
"Color": "green ",
"Doors": 3,
"Paidfor": false
};
1.3 mixed literal
We can mix objects and array literal values to create an array of objects or an object containing arrays. (I am not sure about the specifics)
Someone may be unfamiliar with text in JSON format. I will write JSON data for you below:
{
"Planet ":
{
"Name": "Earth ",
"Type": "small ",
"Info": ["Earth is a small planet, third from the sun", "two-thirds", "climated and landcapes"]
}
}
The following is a simple example:
Function showinfo (Event)
{
VaR planet = This. ID;
VaR scripturl = planet + ". JSON ";
New net. contentloader (scripturl, parsejson );
}
Function parsejson ()
{
VaR name = "";
VaR descrip = "";
VaR jsontxt = net. Req. responsetext;
VaR jsonobj = eval ("(" + jsontxt + ")");
Name = jsonobj. Planet. Name
VaR ptype = jsonobj. Planet. type;
If (ptype)
{
Descrip + = "<H2> '+ ptype +" </H2> ";
}
VaR Infos = jsonobj.planet.info;
Descrip + = "<ul> ";
For (var I in Infos)
{
Descrip + = "<li>" + Infos [I] + "</LI> \ n ";
}
Descrip + = "</ul> ";
Top. showpopup (name, descrip );
}
We use contentloader again to obtain data and assign a callback function parsejson (). The entire response text is a legal JavaScript statement, so we can use a simple call to the eval () function to create an object graph:
VaR jsonobj = eval ("(" + jsontxt + ")");