我有一个像这样的JSON文件:
{ "object": {
"child1": "one",
"child2": "two",
"child3": "three",
"ChildObjects": null
}}
以下是我的c#课程:
public class jsonClass1
{
public string child1 { get; set; }
public string child2 { get; set; }
public string child3 { get; set; }
public ChildObjects ChildObject { get; set; }
}
public class ChildObjects
{
public ChildObjects ChildObject { get; set; }
}
public class ChildObject
{
public string variable1 { get; set; }
public string variable2 { get; set; }
}
因此,如果没有childObjects,我没有任何问题来反序列化json。但有时JSON文件看起来像这样:
{ "object": {
"child1": "one",
"child2": "two",
"child3": "three",
"childObjects": {
"[0]ChildObject": {
"variable1": "value1",
"variable2": "value2"
},
"[1]ChildObject": {
"variable1": "value1",
"variable2": "value2"
}
}
}}
所以现在我的问题是:当对象数量未知(0到x)时如何反序列化?