JavaScriptSerializer error: 'ResponseBody' is not supported for deserialization of an array

北城余情 提交于 2019-12-11 06:37:52

问题


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:

On the Request side -

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

On the Response side -

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

In the .asmx file

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

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.

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.

I do not want to use JSON.NET


回答1:


I think that the issue is that you do not have set the [Serializable] flag as:

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

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

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



回答2:


This is what worked for me -

Change the type of body to string[] instead of List

And change the code to -

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


来源:https://stackoverflow.com/questions/10253436/javascriptserializer-error-responsebody-is-not-supported-for-deserialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!