c# - 如何递归地找到对象子列表中的所有对象?
问题描述
我有一个需要处理的对象。该对象的类型SchemaValidationResults
包含一个列表,该列表List<SchemaValidationResults> NestedResults
可能包含也可能不包含其他SchemaValidationResults
对象。
为了更清楚地说明这一点,我在下面提供了 JSON 表示。在这个 JSON 表示中是一个父对象,它包含一个具有两个子对象的子对象。这些子对象之一具有另一个子对象。
所以我们有:
Parent
Child
Child
Child
Child
我需要能够检索此 JSON 表示中没有任何其他子项的对象。所以在上面的结构中我需要这两个孩子。
Parent
Child
Child <-- Need
Child
Child <- Need
JSON
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties",
"instanceLocation": "#/form_submission/sections",
"keyword": "properties",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions",
"instanceLocation": "#/form_submission/sections/6/questions",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions/properties/102/$ref/properties/answer/type",
"instanceLocation": "#/form_submission/sections/6/questions/102/answer",
"keyword": "type",
"additionalInfo": {
"expected": "string",
"actual": "number"
}
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions/allOf",
"instanceLocation": "#/form_submission/sections/6/questions",
"keyword": "allOf",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions/allOf/0/else/properties/100/properties/answer/maxLength",
"instanceLocation": "#/form_submission/sections/6/questions/100/answer",
"keyword": "maxLength",
"additionalInfo": {
"expected": 0,
"actual": 2
}
}
]
}
]
}
]
}
这种性质使我相信递归将能够帮助我创建一个没有更多子对象的所有子对象的列表。
然而,我开始编写一个方法,我很快意识到我不确定我应该如何构造方法来实现我想要的结果。到目前为止,我有这个:
public List<SchemaValidationResults> GetChildLessError(SchemaValidationResults errors)
{
List<SchemaValidationResults> childLessErrors = new List<SchemaValidationResults>();
foreach(var result in errors.NestedResults)
{
if(result.NestedResults.Count == 0)
{
childLessErrors.Add(result);
}
else
{
foreach(var resultChild in result.NestedResults)
childLessErrors.AddRange(GetChildLessError(resultChild));
}
}
return childLessErrors;
}
这种方法很有效,它生成了一个没有任何子对象的对象列表,但是在我期望的 10 个对象中,它只生成 6 个对象。
下面是我在其上运行此方法的完整 JSON 文件。第一个 JSON 文件只是一个片段。
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties",
"instanceLocation": "#/form_submission/sections",
"keyword": "properties",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/1/properties/questions/properties/3/$ref/properties/answer/enum",
"instanceLocation": "#/form_submission/sections/1/questions/3/answer",
"keyword": "enum"
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions",
"instanceLocation": "#/form_submission/sections/6/questions",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions/properties/102/$ref/properties/answer/type",
"instanceLocation": "#/form_submission/sections/6/questions/102/answer",
"keyword": "type",
"additionalInfo": {
"expected": "string",
"actual": "number"
}
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions/allOf",
"instanceLocation": "#/form_submission/sections/6/questions",
"keyword": "allOf",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions/allOf/0/else/properties/100/properties/answer/maxLength",
"instanceLocation": "#/form_submission/sections/6/questions/100/answer",
"keyword": "maxLength",
"additionalInfo": {
"expected": 0,
"actual": 2
}
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions/allOf/1/else/properties/101/properties/answer/maxLength",
"instanceLocation": "#/form_submission/sections/6/questions/101/answer",
"keyword": "maxLength",
"additionalInfo": {
"expected": 0,
"actual": 3
}
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/6/properties/questions/allOf/4/else/properties/104/properties/answer/maxLength",
"instanceLocation": "#/form_submission/sections/6/questions/104/answer",
"keyword": "maxLength",
"additionalInfo": {
"expected": 0,
"actual": 5
}
}
]
}
]
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/8/properties/questions",
"instanceLocation": "#/form_submission/sections/8/questions",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/8/properties/questions/properties/191/$ref/properties/answer",
"instanceLocation": "#/form_submission/sections/8/questions/191/answer",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/8/properties/questions/properties/191/$ref/properties/answer/minLength",
"instanceLocation": "#/form_submission/sections/8/questions/191/answer",
"keyword": "minLength",
"additionalInfo": {
"expected": 1,
"actual": 0
}
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/8/properties/questions/properties/191/$ref/properties/answer/enum",
"instanceLocation": "#/form_submission/sections/8/questions/191/answer",
"keyword": "enum"
}
]
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/8/properties/questions/allOf/12/else/properties/191/properties/answer/enum",
"instanceLocation": "#/form_submission/sections/8/questions/191/answer",
"keyword": "enum"
}
]
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/9/properties/questions/allOf",
"instanceLocation": "#/form_submission/sections/9/questions",
"keyword": "allOf",
"errors": [
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/9/properties/questions/allOf/0/else/properties/211/properties/answer/maxLength",
"instanceLocation": "#/form_submission/sections/9/questions/211/answer",
"keyword": "maxLength",
"additionalInfo": {
"expected": 0,
"actual": 12
}
},
{
"valid": false,
"keywordLocation": "#/properties/form_submission/properties/sections/properties/9/properties/questions/allOf/1/else/properties/240/properties/answer/maxLength",
"instanceLocation": "#/form_submission/sections/9/questions/240/answer",
"keyword": "maxLength",
"additionalInfo": {
"expected": 0,
"actual": 4
}
}
]
}
]
}
解决方案
你的循环太多了。
这是您的代码的修订版本:
public List<SchemaValidationResults> GetChildLessError(SchemaValidationResults errors)
{
List<SchemaValidationResults> childLessErrors = new List<SchemaValidationResults>();
if(errors.NestedResults.Any())
{
foreach(var resultChild in errors.NestedResults)
{
childLessErrors.AddRange(GetChildLessError(resultChild));
}
}
else
{
childLessErrors.Add(errors);
}
return childLessErrors;
}
推荐阅读
- java - 如何修复错误包 android.support.v7 不存在?
- actions-on-google - 你如何使用 conv.user.entitlements ?它返回一个对象,但不显示数据
- android - Android片段替换
- r - 切片(原始)向量的更快方法?
- javascript - 与 nodejs 相比,在 Django 中使用 request.body 来构建 restful api
- r - R 代码错误 - “参数暗示不同的行数”
- javascript - 无法正确存储数据以在应用程序中实现广度优先搜索,如培根的预言机(对象通信)
- python - 连接 pyqt5 信号时向函数传递附加参数(已传递参数)
- java - 当键的长度相同时,哈希函数返回相同的哈希
- angular - 为什么这个请求会被取消?