Json Deserialize

814 views
Skip to first unread message

Dele O

unread,
Mar 6, 2012, 12:51:09 PM3/6/12
to rest...@googlegroups.com

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; }
    }

    public class Co2Emissions
    {
        public List<EmissionValue> EmissionValues { get; set; }
    }
}
 
 
Thanks in advance for the help.

Cristovão Morgado

unread,
Mar 6, 2012, 12:58:13 PM3/6/12
to rest...@googlegroups.com
i'd say isto something like:

public  class response{
public list<item>{get;set;}
}

public class item{
public int page {get;set;}
public int pages{get;set;}
public string per_page{get;set;}
public string total{get;set;}
public list<subitem> {get;set;}
}

public class subitem{
 public  values indicator{get;set;}
public  values country{get;set;}
public string value{get;set;}
public string decimal{get;set;}
public string date{get;set;}

}

public class values{
public string id{get;set;}
public string value{get;set;}
}


--
Cristovao Morgado
@TheSaintr


Andrew Young

unread,
Mar 6, 2012, 1:01:12 PM3/6/12
to rest...@googlegroups.com
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#.

Dele O

unread,
Mar 6, 2012, 1:05:23 PM3/6/12
to rest...@googlegroups.com
Thanks Cristovao.  I'll give it a try.

Dele O

unread,
Mar 6, 2012, 1:06:02 PM3/6/12
to rest...@googlegroups.com
It definitely is strange.  This is a first for me coming across such a formatted Json response.

Dele O

unread,
Mar 6, 2012, 1:22:04 PM3/6/12
to rest...@googlegroups.com
Looks like i'm still having issues with deserialization. I'm pretty sure the biggest hurdle here is the format of the Json response. here is the error I'm receiving:
"Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray"
And not sure if this will help, but here is a stack trace:
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader)
at Newtonsoft.Json.Linq.JObject.Parse(String json)
at RestSharp.Deserializers.JsonDeserializer.FindRoot(String content)
at RestSharp.Deserializers.JsonDeserializer.Deserialize[T](RestResponse response)
at RestSharp.RestClient.Deserialize[T](IRestRequest request, RestResponse raw)
at RestSharp.RestClient.<>c__DisplayClass2a`1.<ExecuteAsync>b__29(RestResponse response, RestRequestAsyncHandle asyncHandle)
at RestSharp.RestClient.ProcessResponse(HttpResponse httpResponse, RestRequestAsyncHandle asyncHandle, Action`2 callback)
at RestSharp.RestClient.<>c__DisplayClass23.<ExecuteAsync>b__20(HttpResponse r)
at RestSharp.Http.ExecuteCallback(HttpResponse response, Action`1 callback)
at RestSharp.Http.<>c__DisplayClass16.<ResponseCallback>b__14(HttpWebResponse webResponse)
at RestSharp.Http.GetRawResponseAsync(IAsyncResult result, Action`1 callback)
at RestSharp.Http.ResponseCallback(IAsyncResult result, Action`1 callback)
at RestSharp.Http.<>c__DisplayClass3.<GetStyleMethodInternalAsync>b__1(IAsyncResult result)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()

Andy Cutright

unread,
Mar 6, 2012, 1:23:07 PM3/6/12
to rest...@googlegroups.com
Are y'all using the http://json2csharp.com/ tool to generate the classes, btw? Or just hand coding this based on the structure of the JSON? 

My first pass using the json2csharp tool didn't work out to well, and I've dropped down to parsing the response.Content using JObject.Parse and then assembling my instances reading through the JTokens .. 

Andy 

Peter Johanson

unread,
Mar 6, 2012, 1:23:40 PM3/6/12
to rest...@googlegroups.com
Yeah, it is odd. You could use the IRestRequest.OnBeforeDeserialize to
modify the IRestRequest.Content string to be slightly more suitable JSON
format, e.g.:

{
"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

--

Andy Cutright

unread,
Mar 6, 2012, 1:25:47 PM3/6/12
to rest...@googlegroups.com
If it's valid JSON, why should there be any problem parsing it? 

Peter Johanson

unread,
Mar 6, 2012, 1:28:42 PM3/6/12
to rest...@googlegroups.com
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 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

--

Andy Cutright

unread,
Mar 6, 2012, 1:37:25 PM3/6/12
to rest...@googlegroups.com
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 

Peter Johanson

unread,
Mar 6, 2012, 1:43:46 PM3/6/12
to rest...@googlegroups.com
You certainly can "parse" this using Newtonsoft's JSON.NET library very
easily, and manually traverse the object graph of JObject/JArray/string
yourself to handle this particular JSON "format".

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

--

Dele O

unread,
Mar 6, 2012, 1:51:00 PM3/6/12
to rest...@googlegroups.com
I don't think that's a bad idea actually.

Dele O

unread,
Mar 6, 2012, 1:53:37 PM3/6/12
to rest...@googlegroups.com
Hey Andy,
 
The json2csharp tool is unable to convert the json.  Tried it yesterday.  would have been great if it could.

Andy Cutright

unread,
Mar 6, 2012, 2:13:52 PM3/6/12
to rest...@googlegroups.com
Yeah, sure I understand the advantage of letting the framework do the work for you. 

Andrew Young

unread,
Mar 6, 2012, 2:16:05 PM3/6/12
to rest...@googlegroups.com
Where is your json response coming from? Is it from a server under your control or an api outside of your domain?
Reply all
Reply to author
Forward
0 new messages