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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With