The use of $.tojson or the conversion of arrays into JSON types

Source: Internet
Author: User
Tags tojson

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

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.