1.jQuery Part
<script src= "Js/jquery.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
Alert ("JSON");
var user = {"UserID": One, "Name": "Truly", "email": "[email protected]"};
Alert (user. UserID);
Alert (user["Name"]);
alert (user);
}); //Read Simple Object
$ (document). Ready (function () {
var user = {"UserID": One, "Name": {"Fristname": "Truly", "LastName": "Zhu"}, "Emali": "[email protected]"};
Alert (user. Name.fristname);
Alert (user["Name" ["Fristname"]);
});
$ (document). Ready (function () {
var userlist = [
{"UserID": One, "Name": {"FirstName": "Jerry", "LastName": "Tian"}, "Email": "Address1"},
{"UserID": "Name": {"FirstName": "Jerry1", "LastName": "Tian"}, "Email": "Address2"},
{"UserID": +, "Name": {"FirstName": "Jerry2", "LastName": "Tian"}, "Email": "Address3"},
];
Alert (userlist[0). Name.firstname);
alert (userlist.length); //Read complex objects
});
</script>
2.JSON part
JSON objects can be transferred from the background using Webservie asmx aspx in many ways
The JSON object is contained in the []
JSON data variable name to use ""
JSON data needs to be partitioned with {}
The foreground calling code is as follows :
<script type= "Text/javascript" >
$ (document). Ready (function () {
$.ajax ({
URL: "Navigatetree.asmx/getjson",
Type: "POST",
DataType: "JSON",
Data: "{}",
ContentType: "Application/json; Charset=utf-8 ",
Success:function (JSON) {
alert (JSON.D);
var data = eval (' (' + json.d + ') ');
alert (data);
alert (data.length);
alert (Data[0].showcheck);
},
Error:function (x, E) {
alert (X.responsettext);
Alert ("Error");
},
Complete:function (x) {
alert (X.responsetext);
}
});
});
</script>
The background code is as follows
[WebMethod]
public static string Getjson ()
{
string json = "[";
list<tbnavigationtree> t = naviagettreedlinqdal.returnparetntree ();
foreach (Tbnavigationtree model in T)
{
if (model! = T[t.count-1])
{
JSON + = Getjsonbymodel (model) + ",";
}
Else
{
JSON + = Getjsonbymodel (model);
}
}
JSON + = "]";
Json=json. Replace ("'", "/" ");
return JSON;
}
public static string Getjsonbymodel (Tbnavigationtree t)
{
String json = "";
BOOL flag = Naviagettreedlinqdal.ishavingchild (t.id);
JSON = "{"
+ "' ID ': '" + t.id + "',"
+ "' Text ': '" + t.modulename + "',"
+ "' Value ': '" + t.id + "',"
+ "' Showcheck ': true,"
+ "' checkstate ': ' 0 ',"
+ "' HasChildren ':" + flag. ToString (). ToLower () + ","
+ "' Isexpand ': false,"
+ "' ChildNodes ':"; ChildNodes C must be capitalized
if (!flag)
{
JSON + = "null,";
JSON + = "complete"; false} ";
}
Else
{
JSON + = "[";
list<tbnavigationtree> list = Naviagettreedlinqdal.getchild (t.id);
foreach (Tbnavigationtree tree in list)
{
if (Tree! = list[list. COUNT-1])
{
JSON + = Getjsonbymodel (tree) + ",";
}
Else
{
Json+=getjsonbymodel (tree);
}
}
json+= "], ' complete ': true}";
}
return JSON;
}
jquery Read JSON summary