2

I need help or some inspiration for some strange json deserialization.

This is the json I recieve from a service (can't change it, its an external service):

{
   "status":"OK",
   "statuscode":200,
   "payload":{
      "solarforecast":{
         "5876":{
            "2014-06-06 23:00:00":{
               "bh":0,
               "dh":0               
            },
            "2014-06-07 00:00:00":{
               "bh":0,
               "dh":0
            },
            [...]
      }
   }
}

I made a call to get values for a object with id 5876. So if I made a call for object with id 1254, the json changed this way:

[...]
 "solarforecast":{
    "1254":{
       "2014-06-06 23:00:00":{
[...]

I now want to create an c# object from this json code with help of Newton ;) . My frist Problem is that the property name (aka object id) is different for any object call and its a number. My second problem are the sub objects with undefinded count. I think in a well formed json object it has to be somethink like this (see "[" brackets):

      "solarforecast":{
         "5876":[
            "2014-06-06 23:00:00":{
               "bh":0,
               "dh":0               
            },
            "2014-06-07 00:00:00":{
               "bh":0,
               "dh":0
            },
            [...]
          ]
      }

Has anybody a trick or solution how to well deserialize this json into a proper c# class? I try to get somethink like that as result:

public class Payload
{
    [JsonProperty("solarforecast")]
    public SolarForecast SolarForecast;
}

public class SolarForecast
{
    [JsonProperty("???")]
    public IEnumerable<SolarForecastTimeSet> SomeObjectID;
}

public class SolarForecastTimeSet
{
    [JsonProperty("???")]
    public decimal TimeStamp;

    [JsonProperty("dh")]
    public decimal DiffusRadiationHorizontal;

    [JsonProperty("bh")]
    public decimal DirectRadiationHorizontal;
}

Thanks for your help!!

Steffen

Steffen Mangold
  • 1,184
  • 1
  • 20
  • 39
  • 1
    I don't know how this could be deserialized into a properly typed object graph, but you could use a [dynamic object](http://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net) or [dictionary](http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net). – andyp Jun 07 '14 at 09:15
  • First of all, your suggested JSON in the second code block is invalid: `{"123": ["abc":` The parser (or interpreter if `eval`d this with JavaScript) will raise an error at the `:` because in JSON, arrays can only be numerically indexed (and there's no index in array literals). The equivalent of associative arrays is the object notation (as in the original JSON), e.g. `{"123": {"abc":1}}`. The next problem is that you will not get typed classes by parsing any JSON by default. Only arrays (and "standard" object in some cases like PHP). – marekful Jun 07 '14 at 09:22
  • Get the json into a string, use a regex or something to clean it up, and then a json parsing library to parse the cleaned up json string such as json.net: http://james.newtonking.com/json – Matthew Lock Jun 07 '14 at 09:29

1 Answers1

1

Ok figured out how it will work! Object tree has to be:

public class Payload
{
    [JsonProperty("solarforecast")]
    public Dictionary<int, Dictionary<DateTime, SolarForecastTimeSet>> SolarForecast;
}

public class SolarForecastTimeSet
{
    [JsonProperty("dh")]
    public decimal DiffusRadiationHorizontal;

    [JsonProperty("bh")]
    public decimal DirectRadiationHorizontal;
}

Thanks @andyp for his dictionary hint!

Steffen Mangold
  • 1,184
  • 1
  • 20
  • 39