1. HTML page All code
<title></title>
<script src= ". /.. /scripts/jquery-1.4.1.min.js "type=" Text/javascript "></script>
<script src= ". /.. /scripts/jqueryjson.js "type=" Text/javascript "></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#json"). Click (function () {
The names and types of the fields in the array should be the same as those in the class defined in the general handler.
Otherwise there will be problems.
var postdata = new Array ();
Postdata[1] = {id:1, number: "Yes"};
Postdata[2] = {id:2, number: "No"};
var postdata = $.tojson (postdata); //convert array to JSON string
//Deserializes the JSON string, which is just a test of whether the array is converted to a JSON string
var content = $.parsejson (postdata);
$.each (content, function () {
alert (This.number);
});
//post Submit and Process
$.post ("Json.ashx", {"array": PostData}, function (data, status) {
if (status = = "Success") {
alert (data);
}
});
});
})
</script>
<body>
<input type= "button" value= "JSON" id= "json"/>
</body>
2.json.ashx Page All code
<%@ WebHandler language= "C #" class= "JSON"%>
Using System;
Using System.Web;
Using System.Web.Script.Serialization;
Using System.Collections.Generic;
public class Json:ihttphandler {
Public Voidprocessrequest (HttpContext context) {
Context. Response.ContentType = "Text/plain";
//Accept the value coming out
String Sun = context. request["Array"]. ToString ();
//Instantiate JavaScriptSerializer Object
JavaScriptSerializer JSS = new JavaScriptSerializer ();
List<array> a = newlist<array> ();
//convert JSON to other list<array> types
A = JSS. Deserialize (sun,typeof (list<array>)) aslist<array>;
String Meg=null;
foreach (var item in a)
{
Meg + = Item.number;
}
Context. Response.Write (MEG);
}
public class Array
{
public int ID {get; set;}
public string number {get; set;}
}
public bool IsReusable {
get {
return false;
}
}
}
The use of $.tojson or the conversion of arrays into JSON types