C#json对象用于动态属性

VAA*_*AAA 2 c# json

我需要输出这个json:

{
      white: [0, 60],
      green: [60, 1800],
      yellow: [1800, 3000],
      red: [3000, 0]
}
Run Code Online (Sandbox Code Playgroud)

而我试图想象一个模型:

 public class Colors
    {

        public int[] white { get; set; }

        public int[] green { get; set; }

        public int[] yellow { get; set; }

        public int[] red { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

但是属性名称可能会改变,就像白色现在可能是灰色等等.

任何线索?

mac*_*ura 5

你需要的只是一个词典:

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)
Run Code Online (Sandbox Code Playgroud)

结果在这个json:

{
    "white": [0, 60],
    "green": [60, 1800],
    "yellow": [1800, 3000],
    "red": [3000, 0]
}
Run Code Online (Sandbox Code Playgroud)

在这里小提琴