的JavaScriptSerializer错误:'ResponseBody'不支持数组的反序列化 [英] JavaScriptSerializer error: 'ResponseBody' is not supported for deserialization of an array

查看:240
本文介绍了的JavaScriptSerializer错误:'ResponseBody'不支持数组的反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要送一个JSON字符串作为HTTP请求和recieving在响应一个JSON字符串。我已经创建了自己的类,从序列化和反序列化到。他们看起来如下:

I'm sending a json string as an http request and recieving a json string in the response. I've created my own classes to serialize from and deserialize into. They look as follows:

public class RequestHead
{        
    public string source {get; set;}        
    public string dest {get; set;}        
}

public class RequestBody
{
    private List<string> id {get; set;}        
    public bool direction {get; set;}

    public RequestBody()
    {
        this.id = new List<string>();
    }
}

public class RequestObj
{        
    public RequestHead head {get; set;}
    public RequestBody body {get; set;}
}

的响应方 -

public class ResponseHead
{        
    public bool result {get; set;}        
    public float time {get; set;}        
}

public class ResponseBody
{        
    public List<string> body{get; set;}
}

public class ResponseObj
{        
    public ResponseBody body {get; set;}        
    public ResponseHead head {get; set;}        
}

在.asmx文件

            RequestObj request_obj = new RequestObj();
            request_obj.head = head;
            request_obj.body = body;

            var httpWebRequest = (HttpWebRequest)WebRequest.Create("the url");
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "POST";
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = serializer.Serialize(request_obj);
                streamWriter.Write(json);
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
                ResponseObj response_obj = new ResponseObj();
                response_obj = serializer.Deserialize<ResponseObj>(responseText);
                ResponseBody response_body = new ResponseBody();
                response_body = response_obj.body;
                ResponseHead response_head = new ResponseHead();
                response_head = response_obj.head;                    
            }

我已经提到了这个职位,这正是我的场景 - <一个href=\"http://stackoverflow.com/questions/9173811/system-missingmethodexception-error-while-deserialization-of-json-array\">System.MissingMethodException:出错JSON数组的反序列化
但在这里做一个列表中提到的解决方案并不适合我,或者我在做别的soemthing错误的。

I've referred to this post which is exactly the scenario I have - System.MissingMethodException: Error while deserialization of json array But the solution mentioned here of making a List does not work for me, or I'm doing soemthing else wrong.

一切行之有效的请求结束。在反应结束时,主体可以具有1字符串或字符串的数组。不管我有一个或一个数组,我仍然得到同样的错误。

Everything works well on the Request end. On the Response end, the body could have 1 string or an array of strings. Whether I have one or an array, I still get the same error.

推荐答案

这是对我工作 -

和改变code至 -

And change the code to -

            var rbody = serializer.Deserialize<ResponseBody>(responseText);                              
            response_obj.body = new ResponseBody();
            response_obj.body.body = rbody.body.ToArray(); 

这篇关于的JavaScriptSerializer错误:'ResponseBody'不支持数组的反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆