4

I need to output this json:

{
      white: [0, 60],
      green: [60, 1800],
      yellow: [1800, 3000],
      red: [3000, 0]
}

And I was trying to think on a model like:

 public class Colors
    {

        public int[] white { get; set; }

        public int[] green { get; set; }

        public int[] yellow { get; set; }

        public int[] red { get; set; }
    }

But the property names could change, like maybe white can be now gray, etc.

Any clue?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
VAAA
  • 14,531
  • 28
  • 130
  • 253

3 Answers3

7

All you need is a Dictionary:

Dictionary<string, int[]> dictionary = new Dictionary<string, int[]>();

dictionary.Add("white", new int[] { 0, 60 });
dictionary.Add("green", new int[] { 60, 1800 });
dictionary.Add("yellow", new int[] { 1800, 3000 });
dictionary.Add("red", new int[] { 3000, 0 });

//JSON.NET to serialize
string outputJson = JsonConvert.SerializeObject(dictionary)

Results in this json:

{
    "white": [0, 60],
    "green": [60, 1800],
    "yellow": [1800, 3000],
    "red": [3000, 0]
}

Fiddle here

maccettura
  • 10,514
  • 3
  • 28
  • 35
  • No need for that since Json.NET returns a weakly-typed object when parsing arbitrary strings. It's *deserialization* that requires a strongly typed object. – Panagiotis Kanavos Feb 22 '18 at 15:07
  • @PanagiotisKanavos I don't understand what you are saying. If JSON.NET is parsing strings, wouldnt that be _deserialization_? In which case, by your own comment, it would require a strongly typed object? OP wants to generate a specific JSON that is very clearly a string/int[] Dictionary, what solution would be better than that? – maccettura Feb 22 '18 at 15:09
  • A simple `JObject.ToString()` – Panagiotis Kanavos Feb 22 '18 at 15:17
  • @maccettura I think it would be better if you include `JsonConvert.SerializeObject(dictionary)` into the answer. Because now it only shows how to add items to dictionary and finite result – Aleks Andreev Feb 22 '18 at 15:18
  • 1
    The fiddle includes that, I can add it to the answer body. OP had a high enough reputation where I assumed they did not need help with the actual serialization, but how to make a "dynamic" object _to_ serialize – maccettura Feb 22 '18 at 15:19
  • @maccettura thanks for edit, upvoted. Please do not forget that your answer could be usefull not only for OP. Also link to fiddle can become broken after some time, but after your last edit your answer is self contained. – Aleks Andreev Feb 22 '18 at 15:28
2

If you don't mind using an extra library, try Json.Net (ASP.net has this pre-installed). All you have to do is

dynamic result = JsonConvert.DeserializeObject(json);

If I remember correctly, to access a value, use result[0].Value;

1

Json.NET is the library used by almost all ASP.NET projects, including ASP.NET Web API and all ASP.NET Core projects. It can deserialize JSON to a strongly typed object or parse it to a weakly typed JObject, or generate JSON from any object. There is no need to create special classes or objects.

You can serialize any object to a Json string with JsonConvert.SerializeObject

var json=JsonConvert.SerializeObject(someObject);

Or you can use JObject as a dynamic object and convert it directly to a string :

dynamic product = new JObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
product.Price = 4.90m;
product.StockCount = 9000;
product.StockValue = 44100;
product.Tags = new JArray("Real", "OnSale");

Console.WriteLine(product.ToString());
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236