Brief Introduction
Before we know how to use Ajax to return JSON data, we need to understand the following points
1, how JSON to represent the object's
2, how JSON to represent the array of
Copy Code code as follows:
var object = {"Labid": "1", "Labname": "Clothing"};
Usually we use the way above to represent the JSON object, so the array
Copy Code code as follows:
var array = [{"Labid": "1", "Labname": "Fashion"}, {"Labid": "2", "Labname": "Clothing"}, {"Labid": "3", "Labname": "Watch"}]
Array of words as shown above
How Ajax receives JSON data
Suppose the JSON data is like this:
Copy Code code as follows:
private void Getjson ()
{
Sb. Append ("[");
Sb. Append ("{\" artid\ ": \" 1\ "," title\ ": \" Xinjiang Bachu Riot case first trial sentence: 5 Defendants 2 people were sentenced to death \ "," content\ ": \" Tianshan Network news (reporter Nagata Report) August 12, 2013, Xinjiang Kashgar Intermediate People's Court to Bachu "4?23" violent terror case in the wood? ai San and other 5 defendants in the first trial open trial and sentencing, respectively, to organize, lead the crime of terrorist organizations, the crime of illegal manufacture of explosives, the crime of intentional homicide joinder, sentenced to the accused Musa? The death penalty, the deprivation of political rights for life To participate in terrorist organization crime, intentional homicide joinder, sentenced to accused Gehemain Guofur death penalty ... \ "},");
Sb. Append ("{\" artid\ ": \" 2\ "," title\ ": \" The defense University professor said the Army property reorganization and Yu Junshan case related to the ", \" content\ ": \" There are military scholar analysis, to promote the army's clean government, especially to win like Wang Shou, Yu Junshan, a high-ranking military official, expressed the determination to fight corruption at the top of the army and the central government. Former deputy director of the Institute of Political work of the Academy of Military Sciences, Bang Fang, deputy director and Professor of the military political work Department of the National Defense University, has a long and deep research on the military style and the construction of a clean government, and recently he has received an interview with this newspaper. \"}");
Sb. Append ("]");
}
First Type:
Copy Code code as follows:
$ (". Btn"). Live ("Click", Function () {
$.post ("Json.aspx", {action: "Getjson"},
function (data) {
Now the type of data is just string, and now we convert it to a JSON object
var json = eval ("+ Data +"));
if (typeof (json) = = = ' object ') {
$ (JSON). Each (function (i) {
$ ("#wrap"). Append ("<div style=\" width:100%; background: #f00; \ "> The first </div>" +json[i].artid + "" + json[i]. Title + "<br/> Content Abstract:" + json[i].content);
});
}
}
);
});
The second type:
Copy Code code as follows:
$ (". Btn"). Live ("Click", Function () {
$.post ("Json.aspx", {action: "Getjson"},
function (data) {
$ (data). Each (function (i) {
$ ("#wrap"). Append (Data[i].artid + "+ data[i].title +" <br/> Content Summary: "+ data[i].content + <br/>");
});
}, "JSON"
);
});
We've finally set a parameter here, which is datatype, and the JSON string returned after you set up JSON is passed to the client as a JSON object.
Because the data returned is an array object, traverse it and get each object property value