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.
This is what worked for me –
Change the type of body to string[] instead of List
And change the code to –