![](/img/trans.png)
[英]How do I populate the class list which has list of classes for each class item with MySQL data?
[英]How to populate a List which has a class within a class
JSON:
{
"Col1": "John",
"Col2": "Doe",
"Col3Combined": [
{
"Col3": "12",
"Col4Combined": [
{
"Col4": "156-345"
}
],
"Col5Combined": [
{
"Col5": "792-098"
}
]
}
]
},
{
"Col1": "Mike",
"Col2": "Keller",
"Col3Combined": [
{
"Col3": "12",
"Col4Combined": [
{
"Col4": "145-394"
}
],
"Col5Combined": [
{
"Col5": "156-323"
}
]
},
{
"Col3": "15",
"Col4Combined": [
{
"Col4": "909-203"
}
],
"Col5Combined": [
{
"Col5": "121-444",
"Col5": "134-232"
}
]
}
]
}
C#类:
public class Col4Combined
{
public string Col4 { get; set; }
}
public class Col5Combined
{
public string Col5 { get; set; }
}
public class Col3Combined
{
public string Col3 { get; set; }
public List<Col4Combined> Col4Combined { get; set; }
public List<Col5Combined> Col5Combined { get; set; }
}
public class RootObject
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public List<Col3Combined> Col3Combined { get; set; }
}
我正在执行以下操作:
List<RootObject> lr = new List<RootObject>();
lr.Add(new RootObject
{
Col1 = "test",
Col2 = "test2",
Col3Combined = new List<Col3Combined>(); //but then how do I populate it?
}
);
如何在上面的列表中填充Col3Combined
。
您可以为此使用集合初始化程序:
lr.Add(new RootObject
{
Col1 = "test",
Col2 = "test2",
Col3Combined = new List<Col3Combined>
{
new Col3Combined(),
// And others
}
});
基本上,您可以一直这样做:
var lr = new List<RootObject>
{
new RootObject
{
Col3Combined = new List<Col3Combined>
{
new Col3Combined
{
Col4Combined = new List<Col4Combined>
{
new Col4Combined { Col4 = "some string" }
},
Col5Combined = new List<Col5Combined>
{
new Col5Combined { Col5 = "some other string" }
}
}
// And others
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.