C# class for specific JSON / Geckoboard / Trendline widget

280 Views Asked by At

For Geckoboard I want to generate a JSON like this:

{
"item": [
    {
      "value": "274057"
    },
    [
      "38594",
      "39957",
      "35316",
      "35913",
      "36668",
      "45660",
      "41949"
    ]
  ]
}

I am struggling with the missing property before the array.

How must the C# Class look like to serialize to that?

2

There are 2 best solutions below

0
On BEST ANSWER

Those works fine for dynamic languages, like javaScript, but it is not so easy to do same thing in C#. The best i can see here - this whole JSON will map to following class structure:

public class RootItem
{
    public IEnumerable<object> Item {get;set;}
}

public class ValueItem 
{
    public string Value {get;set;}
}

And basically next you will need a custom serializer rules or parameter binder (depending where you are using it), that will take this JSON and manually fill RootItem.Item collection with either collection of ValueItem or IEnumerable<string>.

0
On

Paste your json to json2csharp, and generate a suitable class definition for serializing that json there :

public class RootObject
{
    public List<object> item { get; set; }
}

Json item property defined as an array containing mixed data type (singular object and array), and one possible data type to hold both type in .NET is object.