1. html页面全部代码

<html>
<head>
   
<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 () {

             //数组里的字段的命名和类型要和一般处理程序里定义的类里的变量要一样

             //否则会出问题
               
var postdata = new Array();
               
postdata[1] = { id: 1, number: "yes" };
               
postdata[2] = { id: 2, number: "no" };

var postData = $.toJSON(postdata);  //把数组转换成json字符串

//将json字符串反序列化,这个只是测试一下数组是否转换成json字符串

var content = $.parseJSON(postData);
               
$.each(content, function () {
                   
alert(this.number);
               
});

//post提交并处理

$.post("json.ashx", { "array": postData }, function (data, status)
{
                   
if (status == "success") {
                       
alert(data);
                   
}
               
});

});
       
})
   
</script>
</head>
<body>
<input type="button" value="json"
id="json"/>
</body>
</html>

2.json.ashx页面全部代码

<%@ WebHandler Language="C#" class="json"
%>

using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;

public class json : IHttpHandler {
   
    public void
ProcessRequest (HttpContext context) {
       
context.Response.ContentType = "text/plain";

//接受出过来的值

string sun = context.Request["array"].ToString();

//实例化JavaScriptSerializer对象
       
JavaScriptSerializer jss = new JavaScriptSerializer();
       
List<array> a = new
List<array>();

//把json转换其他list<array>类型
       
a = jss.Deserialize(sun,
typeof(List<array>)) as
List<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;
       
}
    }

}

随机推荐

  1. TTL值的含义以及与域名DNS TTL值的区别

    TTL值的含义以及与域名TTL值的区别 本文来源于时光漂流瓶 http://www.9usb.net , 原文地址: http://www.9usb.net/201004/ttl-yuyuming-t ...

  2. 【nodejs】jade模板入门

    使用jetbrians webstom创建空项目 1.创建package.json 引用依赖配置 { "name": "demojade", "des ...

  3. IOS 控件的生命周期

    ViewController的生命周期包括: Initialize ViewDidLoad ViewWillAppear ViewDidAppear ViewWillDisappear ViewDid ...

  4. 包装设计模式的实现以改进BufferedReader中的readLine方法为例

    实现与目标对象相同的接口     BufferedReader 定义一个变量记住目标对象 定义一个构造器接收被增强对象 覆盖需要增强的方法 对于不想增强的方法,直接调用目标对象的方法. package ...

  5. WPF中使用ValueConverter来实现“范围条件触发器”

    在WPF中,我们知道界面层可以通过Trigger触发器实现“条件”——“赋值”的功能 属性触发器Property Trigger:当Dependency Property的值发生改变时触发.数据触发器 ...

  6. BAT CMD 批处理文件脚本 -1

    http://www.cnblogs.com/linglizeng/archive/2010/01/29/Bat-CMD-ChineseVerion.html 1.               综述 ...

  7. iPhone 6 & iPhone 6 Plus适配

    转载请注明出处: http://www.cnblogs.com/dokaygang128/p/4049461.html Apple 今年发布了两款新的iPhone机器,iPhone 6 和iPhone ...

  8. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律

    题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...

  9. int a[5]={1,2,3,4,5}; int *p=(int*)(&a+1); printf("%d",*(p-1)); 答案为什么是5?

    这个问题的关键是理解 &a a是一个数组名,也就是数组的首地址.对a进行取地址运算符,得到的是一个指向数组的指针!!!!这句话尤为重要!也就相当于int (*p) [5] = &a;p ...

  10. java 页面换行处理

    在taxtarea中输入的文本.如果含有回车或空格.在界面上显示的时候则不哪么正常.回车消失了,空格变短了. 如何解决这个问题呢.有2种方法. 1.使用<pre>标签 w3c对pre元素是 ...