I have the following JSON returning from a Java service
{"Test":{ "value": 1, "message": "This is a test" }}
I have the following C # class
class Test { public int value { get; set; } public String message { get; set; } }
However, since the root tag "Test" is returned, I cannot directly deserialize this with
Test deserializedTest = JsonConvert.DeserializeObject<Test>(jsonString);
I believe that for this you need to wrap the Test class inside another class. Is there an easy way to avoid this other than
JToken root = JObject.Parse(jsonString); JToken testToken = root["Test"]; Test deserializedTest = JsonConvert.DeserializeObject<Test>(testToken.toString());
Finally, I have a second question. Most services that I call can also return an Exception object. I decided that I would read the โrootโ JSON tag to determine how to deserialize the object. How to get the first root tag and / or is there a better, more elegant way to handle exceptions from a service?
thanks
Shaun
source share