jquery Read JSON summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.