JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write, and easy to parse and generate by machines. It is based on JavaScript. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, and so on ). These features make JSON an ideal data exchange language.
JSON data has a strict format, which must be followed before it can be parsed. There are two main structures:
① Set of "name/value" PairsIt is understood as an object, structure, and associated array in different languages.
② Ordered list of valuesIn most languages, it is understood as an array. JSON indicates name/Value Pair: {"firstname": "Brett "}
Multiple name/value pairs are listed together: {"firstname": "Brett", "lastname": "McLaughlin "}
In terms of syntax, this is not a great advantage over name/value pairs, but JSON is easier to use and more readable in this case. To represent a group of values, JSON not only improves readability, but also reduces complexity: {"employees ":[
{"Firstname": "Brett", "lastname": "McLaughlin"},
{"Firstname": "Jason", "lastname": "Hunter"},
{"Firstname": "elliotte", "lastname": "Harold"}
]}
What is the JSON data process used by Asp.net?
1 aspx to CS
2. Process aspx values in CS. After some processing, the values are processed in JSON format.
3. Return Aspx. The processed data that can be obtained in Aspx. Obtain the value in the form of a key-value pair.
Example 1:
Public class jsonhandler: ihttphandler
{
Public void processrequest (httpcontext context)
{
Context. response. contenttype = "text/plain ";
String data = "[{Name: \" Tom \ ", age: \" 26 \ "},{ name: \" Jim \ ", age: \ "27 \"}] ";
Context. response. Write (data );
} Public bool isreusable
{
Get
{
Return false;
}
}
}
② Parse JSON data at the front end
<Head runat = "server">
<Title> </title> <SCRIPT src = "JS/jquery-1.3.2.js" type = "text/JavaScript"> </SCRIPT> <SCRIPT type = "text/JavaScript">
$ (Function (){
$. Getjson(
"Jsonhandler. ashx ",
Function (data ){
$. Each (data, function (I ){
$ ("# Cat-List"). append ("<li> name:" +
Data [I]. Name
+ "Age:" +
Data
[I]. Age+ "</LI> ")
});
});
});
</SCRIPT></Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Ul id = "cat-list"> </ul>
</Div>
</Form>
</Body>
Example 2:
Aspx
$. Getjson ("ajax/test. aspx", {ID: 1}, function (data ){
$. Each (data, function (I ){
Data [I]...
})
})
Test. CS
Public partial class getpoints: system. Web. UI. Page
{
List <student> List = Getlist (ID );
Javascriptserializer JSS = new javascriptserializer ();
Response. Write (JSS. serialize (list ));
Response. End ();
}
We can see the serialize method of the javascriptserializer object. What is the purpose of this method?
Model. Student itself has a name and other fields. The serialize method processes the data object into JSON format data.
[{"Num": "111", "name": "XY", "location": "China"}] ....... Pass
Data [I]. NameTo obtain the value. Of course we can use the $. Ajax method to obtain the processed JSON data. $. Ajax ({
Type: "Get ",
Datatype: "JSON ",
Data: {ID: JSON [I]. ID },
Async: false, URL: "ajax/test. aspx"Success: function (data ){
}
});