我将 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:json数组反序列化时出错 但是这里提到的制作列表的解决方案对我不起作用,或者我做错了其他事情。
在请求端一切正常。在响应端,正文可以有 1 个字符串或字符串数组。无论我有一个还是一个数组,我仍然得到同样的错误。