简体   繁体   中英

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

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

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

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM