json C#中的嵌套序列化

yvgpqqbh  于 2023-01-14  发布在  C#
关注(0)|答案(2)|浏览(135)

我被要求在json中提供以下格式的数据:

{
    "testsystem": "1.0.0",
    "playback": { 
       "1": {
          "cue": 1,
          "state": "Stopped",
          "label": "Hello"
       },
       "2": {
          "cue": 100,
          "state": "Running",
          "label": "Hello"
       }
    }
}

我一直在使用Newtonsoft json来序列化信息,但有几点让我感到困惑。

public class Company
        {
            [JsonProperty("testsystem")]
            public string testsystem { get; set; }

            [JsonProperty("playback")]
            public pb playback { get; set; }

        }

        public class pb
        {
            [JsonProperty("playback")]
            public int playback { get; set; }

            //[JsonProperty("cue")]
            public cue cue { get; set; }
        }

        public class cue
        {
            [JsonProperty("cue")]
            public string number { get; set; }

            [JsonProperty("state")]
            public string state { get; set; }

            [JsonProperty("label")]
            public string label { get; set; }

        }

我的类声明

Company thisCompany = new Company
{
    testsystem = "1.0.0",
    for (PB = 1; PB < 7; PB++)
    {
       playback = new pb
       {
           cue = new cue
           {
           number = Globals.QNumber[1],                             
           state = Globals.QState[1],
           label = Globals.QLabel[1]                             
           }
       }
}
string json = JsonConvert.SerializeObject(thisCompany);

我试图序列化的代码段
所以我的第一个问题是,我不知道如何嵌套“playback”部分,以便像示例中那样有2个部分。我尝试添加一个for循环but(如上面的代码所示),但这似乎使playback = new pb行脱离了上下文?
第二个问题是,我最后得到了一个double“回放部分,如下面的输出所示(我退出for循环以使此位工作)

{
  "testsystem": "1.0.0",
  "playback": {
    "playback": 0,
    "cue": {
      "cue": "34",
      "state": "Running",
      "label": "Hello World"
    }
  }
}

我觉得这一定很简单,但我只是错过了最简单的事情和过度思考。提前感谢任何想法和建议。

8ljdwjyq

8ljdwjyq1#

我不认为你的模型很适合json这个例子,如果你的“playback”对象是一个字典,它应该看起来更像这样:

public class Company
{
    [JsonProperty("testsystem")]
    public string Testsystem { get; set; }

    [JsonProperty("playback")]
    public Dictionary<string, Playback> Playback { get; set; }
}

public class Playback
{
    [JsonProperty("cue")]
    public int Cue { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }

    [JsonProperty("label")]
    public string Label { get; set; }
}

然后,您可以创建对象并序列化它们,如下所示:

public static string CreateCompanyJson()
{
    var thisCompany = new Company
    {
        Testsystem = "1.0.0",
        Playback = new Dictionary<string, Playback>()
    };
    for (var pb = 1; pb < 7; pb++)
    {
        thisCompany.Playback[$"{pb}"] = new Playback
        {
            Cue = pb,
            State = $"State-{pb}",
            Label = $"Label-{pb}"
        };
    }
     return JsonConvert.SerializeObject(thisCompany, Formatting.Indented);
}

这将生成格式与示例类似的json:

{
  "testsystem": "1.0.0",
  "playback": {
    "1": {
      "cue": 1,
      "state": "State-1",
      "label": "Label-1"
    },
    "2": {
      "cue": 2,
      "state": "State-2",
      "label": "Label-2"
    },
    "3": {
      "cue": 3,
      "state": "State-3",
      "label": "Label-3"
    },
    "4": {
      "cue": 4,
      "state": "State-4",
      "label": "Label-4"
    },
    "5": {
      "cue": 5,
      "state": "State-5",
      "label": "Label-5"
    },
    "6": {
      "cue": 6,
      "state": "State-6",
      "label": "Label-6"
    }
  }
}
shyt4zoc

shyt4zoc2#

如果您只需要一个JSON字符串,则不需要任何类

var company = new JObject
    {
        ["testsystem"] = "1.0.0",
        ["playback"] = new JObject()
    };
    for (var pb = 1; pb < 7; pb++)
    {
        ((JObject)company["playback"]).Add(
        CreateJsonProperty(pb.ToString(), pb * 10, $"state{pb}", $"label{pb}")
        );
    }

    var json = company.ToString();

public static JProperty CreateJsonProperty(string pb, int cue, string state, string label)
{
    return new JProperty(pb, new JObject
    {
        ["cue"] = cue,
        ["state"] = state,
        ["label"] = label
    });
}

相关问题