I have a problem with deserializing JSON data from an external REST service. Depending on the request, I get the information wrapped in one, two or even three layers of wrappers. An example is:
{
programsResponse: {
programs: [
{
id: 1283,
name: "program name"
},
{
id: 1234,
name: "another program name"
}
]
}
}
This is one of the simplest examples I could come up with, but if you toss another wrapper around this, you’ll see how this can get kind of complicated.
One of the ways I’ve solved this before is to make a ton of wrapper-classes, but it feels like the biggest hack ever. Is there any way to deserialize, let’s say, from a specified element-name? I know that DataContractJsonSerializer has a constructor for just this in .net 4, but not for Windows Phone. Any ideas as to how I can do this? I’m open for using RestSharp, but I haven’t found a way to do this there as well.
In the case of my example, I DO have a Program model to consume the data.
Any help would be greatly appreciated!
You could do it using JSON.NET (RestSharp depends on it) and JObject from it. Here is a good article: http://james.newtonking.com/pages/json-net.aspx