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

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

1. 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 a = new List();

        //把json转换其他list类型
        a = jss.Deserialize(sun, typeof(List)) as List;
        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.     ///   
  11.     /// Summary description for PersonalizerPreViewHandler  
  12.     ///   
  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 optionBOs = new List();  
  22.             optionBOs = jss.Deserialize(optionBOStr, typeof(List)) as List;  
  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. }  

你可能感兴趣的:(jquery,java,javascript)