将json数组发送到ashx处理程序

时间:2013-11-06 10:37:00

标签: jquery asp.net json ashx

我有这段代码,当我执行它时,它会一直显示相同的错误:无效的JSON原语:标题。

客户端:

        var title = new Array();

        ...

        for (var i = 0; i < names.length; ++i) {
            title[i] = '{ "titulo' + i + ':"' + names[i] + '"}';
        }

        $("#gif").show();

        $.ajax({
            async: true,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: "POST",
            data: { titles: title },
            url: "../handlers/saveUpload.ashx",
            success: function (msg) {                    
                $("#gif").hide();
            }
        });

服务器端:

        context.Response.ContentType = "application/json";
        var data = context.Request;
        var sr = new StreamReader(data.InputStream);
        var stream = sr.ReadToEnd();

        var javaScriptSerializer = new JavaScriptSerializer();

        var arrayOfStrings = javaScriptSerializer.Deserialize<string[]>(stream);

        foreach (var item in arrayOfStrings)
        {
            context.Response.Write(item);
        }

此致

2 个答案:

答案 0 :(得分:3)

在这篇文章中解决:convert array to json and receive in ashx handler

  

string []不是该操作的有效泛型类型; string没有无参数构造函数,因此当序列化程序尝试新的构造函数时,它会失败。此外,你已经有一个来自sr.ReadToEnd()的字符串,所以你不是真的反序列化,它更像是你要求它为你解析和拆分字符串,这是不可能的。

JavaScriptSerializer是非常无情的,说实话,我总是在尝试对这样的数组进行反序列化时彻底撕掉我的头发......你最好在服务器端定义一个DTO类来处理映射:

 [Serializable]
 public class Titles
 {
    public List<Title> TheTitles { get; set; } 
 }

 [Serializable]
 public class Title
 {
    public string title { get; set; }
 }

所以现在你的处理程序看起来像这样:

 public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "application/json";
                var data = context.Request;
                var sr = new StreamReader(data.InputStream);
                var stream = sr.ReadToEnd();    
                var javaScriptSerializer = new JavaScriptSerializer();
                var PostedData = javaScriptSerializer.Deserialize<Titles>(stream);    
                foreach (var item in PostedData.TheTitles )
                {
                   //this will write SteveJohnAndrew as expected in the response 
                   //(check the console!)
                   context.Response.Write(item.title);
                }
            }
            catch (Exception msg) { context.Response.Write(msg.Message); }
        }

你的AJAX是这样的:

 function upload() 
        {
           //example data
            var Titles = [
                {'title':'Steve'}, {'title':'John'}, {'title':'Andrew'}
            ];    
            var myJSON = JSON.stringify({ TheTitles: Titles });    
            console.log(myJSON);    
            $.ajax({
                async: true,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                type: "POST",
                data: myJSON,
                url: "jsonhandler.ashx",
                success: function (msg) {
                    console.log(msg);
                }     
            });
        }

注意DTO类的定义如何与JSON对象属性的定义完全匹配,如果没有,则反序列化将不起作用。

希望有所帮助。

答案 1 :(得分:0)

只是为Diogo的回答添加一个更明显的评论,这是正确的,但在我的情况下需要在父类的子列表中添加JsonProperty

[Serializable]
 public class Titles
 {
    [JsonProperty("Titles")] 
    public List<Title> TheTitles { get; set; } 
 }