Newtonsoft.Json 与 parseJSON 运用一则

function statusJSON(tp) {
   

    //从TABKE获取数据

    var batchArray = null;   

    var table = $("#tbRepeater");
    table.find("tr").each(function (index) {
        if (lst == null) {
            batchArray = '{Id:"' + table.find("tr").eq(index).find("td #batchId").text() + '"}';
        }
        else {
            batchArray= batchArray+ "," + '{Id:"' + table.find("tr").eq(index).find("td #batchId").text() + '"}';
        }
    });


    if (batchArray != null) {

        $.ajax({
            type: 'POST',
            url: 'StatusHandler.ashx',
            cache: false,
            datatype: 'json',
            data: '{Tp:"' + tp + '",Batches:[' + batchArray+ ']}', 
            error: function () {
                alert("Refresh Failed!");
            },
            success: function (jsondata) {

 

                 //转化JSON
                var data = $.parseJSON(jsondata);

 

                //渲染TABLE Status 字体颜色
                var table = $("#tbRepeater");
                $(data).each(function (index) {
                    table.children().find("td #batchId[title='" + data [index].Id + "']").parent().siblings().find("#status").css("color", data[index].Color);
                });

 

                alert("Refresh Done");
            }
        });
    }

}

 

================================================================

 

[Serializable]
public class BatchJSON
{
    public string Id { get; set; }
    public string Color { get; set; }

    public string Tp { get; set; }
    public BatchJSON [] Batches { get; set; }
}

 

 

================================================================

 

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

 

        var batchIds = context.Request.Form[0];
        BatchJSON batchJSON = Newtonsoft.Json.JsonConvert.DeserializeObject(batchIds, typeof(BatchJSON)) as BatchJSON;

      

        foreach (BatchJSON batch in batchJSON.Batches)
        {
                //TODO
        }
      
        context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(batchJSON.Batches));

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

 

 

posted @ 2012-12-01 17:18  Yu  阅读(279)  评论(0编辑  收藏  举报