[英]Nested serialisation in C#
我被要求按以下格式提供 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; }
}
我的 class 報關單
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);
我試圖序列化的代碼部分
所以我的第一個問題是我無法弄清楚如何嵌套“播放”部分,以便在示例中有 2 個。 我嘗試添加一個 for 循環但是(如我上面的代碼所示)但這似乎使 playback = new pb 行脫離了上下文?
第二個問題是我在下面的 output 中顯示了一個雙“播放部分”
{
"testsystem": "1.0.0",
"playback": {
"playback": 0,
"cue": {
"cue": "34",
"state": "Running",
"label": "Hello World"
}
}
}
我覺得這一定很簡單,但我只是錯過了最簡單的事情和想得太多。 提前感謝您的任何想法和建議。
我認為您的 model 不太適合示例 json。您的“播放”object 是一本字典,它應該看起來更像這樣:
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; }
}
然后你可以創建 object 並像這樣序列化它們:
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"
}
}
}
如果你只需要一個 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
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.