JavaScriptSerializer错误:反序列化不支持'ResponseBody'

时间:2012-04-20 20:56:00

标签: c# asp.net json deserialization

我正在发送一个json字符串作为http请求,并在响应中收到一个json字符串。我创建了自己的类来序列化和反序列化。他们看起来如下:

在请求方 -

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

我已经提到过这篇文章,这正是我所拥有的情景 - System.MissingMethodException: Error while deserialization of json array 但是这里提到的制作List的解决方案对我来说不起作用,或者我做错了其他的事情。

请求结束时一切正常。在Response端,body可以有1个字符串或一个字符串数组。无论我有一个还是一个数组,我仍然会得到同样的错误。

我不想使用JSON.NET

2 个答案:

答案 0 :(得分:1)

我认为问题在于您没有将[Serializable]标记设置为:

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

答案 1 :(得分:0)

这对我有用 -

将body的类型更改为string []而不是List

将代码更改为 -

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