0

This is the JSON string -

"{\"body\":[\"VAL1\",\"VAL2\"],\"head\":{\"result\":true,\"time\":3.859}}"

These are my classes -

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

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

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

And the code -

JavaScriptSerializer serializer = new JavaScriptSerializer();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    responseText = streamReader.ReadToEnd();
}
ResponseObj response_obj = new ResponseObj();

ResponseHead rhead = new ResponseHead();
rhead = serializer.Deserialize<ResponseHead>(responseText); //not working

The resultant ResponseHead object has values:

result: false 
time: 0.0 

It is not able to map the values correctly, but i'm not sure why. The ResponseBody values are coming in correctly.

Please help!

neuDev33
  • 1,573
  • 7
  • 41
  • 54
  • if you put the values inside a string in your json you will see them – ControlAltDel Apr 23 '12 at 16:27
  • did the [Serializable] work on your previus question ? http://stackoverflow.com/questions/10253436/javascriptserializer-error-responsebody-is-not-supported-for-deserialization/10256768#10256768 – Aristos Apr 23 '12 at 16:29
  • nope, I've added the answer that worked there. It was just a change of type. Thanks for the answer though, it may have saved me from future errors. – neuDev33 Apr 23 '12 at 16:33

1 Answers1

4

Looks like you are trying to read ResponseObj (which is top level object in your JSON), but coded for ResponseHead. Following should work:

var wholeObject = serializer.Deserialize<ResponseObj>(responseText);
rhead = wholeObject.head;
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • This gives me an error saying - Deserialization of an array not supported, since the ResponseBody is an array. Hence, I am trying to get the values separately. It does work if I remove the ResponseBody from ResponseObj and only keep ResponseHead and then get the value for the object as a whole. However, getting the value for ResponseHead separately isn't working. – neuDev33 Apr 23 '12 at 16:35
  • 1
    Just change ResponseBody to an array of sting. If you cant change the json object. – Tim B James Apr 23 '12 at 18:38
  • 1
    If you can't get built in serializers (DataContractJsonSerializer and JavaScriptSerializer) to work with your JSON consider [JSON.net](http://json.codeplex.com/) which provides much more options. – Alexei Levenkov Apr 23 '12 at 19:00
  • @TimBJames: I considered using JSON.NET, which gave me a more descriptive error, that made me realize that the issue was to not wrap the string[] into the ResponseBody object. I got rid of that object and directly made my ResponseObject have a string[] as the body. Just came back to SO and read Tims reply asking me to do just that! After this, using JavaScriptSerializer worked as well. – neuDev33 Apr 23 '12 at 19:25