AJAX $.toJSON的用法或把数组转换成json类型

13 篇文章 0 订阅
6 篇文章 0 订阅

原文链接 http://fhuan123.iteye.com/blog/1687412

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;
        }
    }

}

 

 

我的代码:

Js代码   收藏代码
  1. function ClickPreViewHandle(obj) {  
  2.         var divid = $(obj).attr("surfaceid").replace("btnPV""div")  
  3.         var inputvalues = GetSurfaceTextInputValue(divid);  
  4.         var inputvaluesJS = $.toJSON(inputvalues);  
  5.         jQuery.blockUI({ message: "Generation Image...", css: { padding: 25, color: '#fff', border: '3px solid #aaa', backgroundColor: '#507691'} });  
  6.         $.ajax({  
  7.             type: "post",  
  8.             url: "/PersonalizerPreViewHandler.ashx",  
  9.             //dataType: "json",  
  10.             data: { 'values': inputvaluesJS },  
  11.             success: function (data) {  
  12.                 var leftA = divid.replace("div""divimg");  
  13.                 $("#" + leftA + " img").attr("src", data);  
  14.                 jQuery.unblockUI();  
  15.             },  
  16.             error: function (err) {  
  17.                 alert("error:" + err);  
  18.                 jQuery.unblockUI();  
  19.             }  
  20.         });  
  21.         return false;  
  22.     }  
 Handler
C#代码   收藏代码
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Script.Serialization;  
  6. using THY.Allure.BusinessEntities.BaseEntities;  
  7.   
  8. namespace THY.Allure.Web  
  9. {  
  10.     /// <summary>  
  11.     /// Summary description for PersonalizerPreViewHandler  
  12.     /// </summary>  
  13.     public class PersonalizerPreViewHandler : IHttpHandler  
  14.     {  
  15.   
  16.         public void ProcessRequest(HttpContext context)  
  17.         {  
  18.             context.Response.ContentType = "text/plain";  
  19.             string optionBOStr = context.Request.Params["values"];  
  20.             JavaScriptSerializer jss = new JavaScriptSerializer();  
  21.             List<ProductDetailOptionsBO> optionBOs = new List<ProductDetailOptionsBO>();  
  22.             optionBOs = jss.Deserialize(optionBOStr, typeof(List<ProductDetailOptionsBO>)) as List<ProductDetailOptionsBO>;  
  23.               
  24.             context.Response.Write("~/media/images/cordial/product-surface-images/Postcards/PC1476/010.ashx");  
  25.         }  
  26.   
  27.         public bool IsReusable  
  28.         {  
  29.             get  
  30.             {  
  31.                 return false;  
  32.             }  
  33.         }  
  34.     }  
  35. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值