I need some help deserializing the following Json response. I have not had any issues in the past, but i think it's because the Json being returned seems out of the ordinary. Here is the Json:
[
{
"page": 1,
"pages": 1,
"per_page": "50",
"total": 12
},
[
{
"indicator": {
"id": "EN.ATM.CO2E.KT",
"value": "CO2 emissions (kt)"
},
"country": {
"id": "US",
"value": "United States"
},
"value": null,
"decimal": "0",
"date": "2011"
},
{
"indicator": {
"id": "EN.ATM.CO2E.KT",
"value": "CO2 emissions (kt)"
},
"country": {
"id": "US",
"value": "United States"
},
"value": null,
"decimal": "0",
"date": "2010"
}
]
]
is this class structure correct?
namespace Coolnamespace
{
public class ValueDetail
{
public double Value { get; set; }
public string Date { get; set; }
}
public class EmissionValue
{
public ValueDetail Detail { get; set; }
}
{
"metadata": { "pages": ... },
"list": [
]
}
And then that would be easier to map to .NET classes to deserialize too.
A little more manual, but keeps you from having to do all the
deserialization yourself, since the server is not in your control.
-pete
On Tue, Mar 06, 2012 at 10:06:02AM -0800, Dele O wrote:
> It definitely is strange. This is a first for me coming across such a
> formatted Json response.
> On Tuesday, March 6, 2012 1:01:12 PM UTC-5, ayoung wrote:
>
> The object graph here is kind of strange. The root is an array that
> has an object and another array. Boils down to this [ {obj}, [] ]
>
> Pretty tough to represent something like that in C#.
> References
>
> 1. mailto:dolo...@gmail.com
--
In particular, RestSharp doesn't deserialize to non-generic lists, which
is what would be required in order to support this particular JSON,
e.g.:
client.Execute<ArrayList> (new RestRequest ("/crazy/json"));
Since the JSON array contains a mix of a random object, and a second
nested list.
-pete
> > On Tue, Mar 6, 2012 at 9:51 AM, Dele O <[1][2]dolo...@gmail.com>
> > 1. mailto:[3]dolo...@gmail.com
> --
>
> References
>
> 1. mailto:pe...@peterjohanson.com
> 2. mailto:dolo...@gmail.com
> 3. mailto:dolo...@gmail.com
--
The advantage that RestSharp brings over this is strongly-typed
deserialization to your own classes. In order to get the
"strongly-typed" portion of that, RestSharp relies on the generic
parameter of List<T> to know which of your classes it should deserialize
to.
In this case, the list is a grab-bag of types, so there's no way to
easily "automagically" (as RestSharp normally does) map this to anything
strongly-typed that makes sense.
Does that clarify things a bit?
-pete
On Tue, Mar 06, 2012 at 10:37:25AM -0800, Andy Cutright wrote:
> Is this just a limitation of the library, or has this something to do
> with C#? I'm new to C# and this library,
>
> Cheers,
>
> Andy
> On Tue, Mar 6, 2012 at 10:28 AM, Peter Johanson
> <[1]pe...@peterjohanson.com> wrote:
>
> There's no problem with parsing it, but more an issue with the
> format
> not specifically working with the *deserialization to classes* as
> implemented in RestSharp.
> In particular, RestSharp doesn't deserialize to non-generic lists,
> which
> is what would be required in order to support this particular JSON,
> e.g.:
> client.Execute<ArrayList> (new RestRequest ("/crazy/json"));
> Since the JSON array contains a mix of a random object, and a second
> nested list.
> -pete
>
> On Tue, Mar 06, 2012 at 10:25:47AM -0800, Andy Cutright wrote:
> > If it's valid JSON, why should there be any problem parsing it?
> >
> > On Tue, Mar 6, 2012 at 10:23 AM, Peter Johanson
>
> <[1][2][3]dolo...@gmail.com>
> > > 1. mailto:[3][4]dolo...@gmail.com
> > --
> >
> > References
> >
> > 1. mailto:[5]pe...@peterjohanson.com
> > 2. mailto:[6]dolo...@gmail.com
> > 3. mailto:[7]dolo...@gmail.com
> --
>
> References
>
> 1. mailto:pe...@peterjohanson.com
> 2. mailto:pe...@peterjohanson.com
> 3. mailto:dolo...@gmail.com
> 4. mailto:dolo...@gmail.com
> 5. mailto:pe...@peterjohanson.com
> 6. mailto:dolo...@gmail.com
> 7. mailto:dolo...@gmail.com
--