我正在尝试在此 JSON 中找到组件的状态。
{
"data": {
"id": 0,
"name": "name1",
"childFolders": [
{
"id": 60,
"name": "name2",
"childFolders": [
{
"id": 72,
"name": "Simulator",
"childFolders": [
{
"id": 235,
"name": "generic data pumper",
"childFolders": [],
"childComponents": [
{
"id": 14,
"name": "TCP Client",
"state": "ERROR",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 46256
},
{
"id": 99,
"name": "A4_Timer",
"state": "STOPPED",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
},
{
"id": 102,
"name": "A2_Timer",
"state": "STOPPED",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
},
{
"id": 108,
"name": "A8_Timer",
"state": "STOPPED",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
},
{
"id": 113,
"name": "Timer MRO",
"state": "RUNNING",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
},
{
"id": 119,
"name": "A1_Timer",
"state": "STOPPED",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
},
{
"id": 121,
"name": "A3_Timer",
"state": "STOPPED",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
},
{
"id": 124,
"name": "TImer URO",
"state": "STOPPED",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
},
{
"id": 236,
"name": "Demographics Reporter Generic",
"state": "RUNNING",
"type": "ROUTE"
},
{
"id": 299,
"name": "TImer for UIS",
"state": "STOPPED",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
}
]
}
],
"childComponents": []
},
{
"id": 920,
"name": "Get_JavaScript_Counter_Value",
"childFolders": [],
"childComponents": [
{
"id": 916,
"name": "HTTP Server",
"state": "RUNNING",
"type": "COMMUNICATION_POINT",
"inboundQueueSize": 0,
"outboundQueueSize": 0
},
{
"id": 917,
"name": "Get_JavaScript_Counter_Value",
"state": "RUNNING",
"type": "ROUTE"
}
]
}
],
"childComponents": []
}
],
"childComponents": []
},
"error": null
}
我从 json2sharp.com 为 JSON 生成了类。
public class ChildFolder2
{
public int id { get; set; }
public string name { get; set; }
public List<object> childFolders { get; set; }
public List<object> childComponents { get; set; }
}
public class ChildFolder
{
public int id { get; set; }
public string name { get; set; }
public List<ChildFolder2> childFolders { get; set; }
public List<object> childComponents { get; set; }
}
public class Data
{
public int id { get; set; }
public string name { get; set; }
public List<ChildFolder> childFolders { get; set; }
public List<object> childComponents { get; set; }
}
public class RootObject
{
public Data data { get; set; }
public object error { get; set; }
}
JSON 存储为字符串。我正在使用 JSON.NET。但是任何不使用 JSON.NET 的解决方案也会有所帮助。所以现在如果我有一个组件的名称,我必须找到该组件的状态。组件的类型将为"COMMUNICATION_POINT"。提前非常感谢你。
首先,你的类应该看起来像这样:
public class Component
{
public int id { get; set; }
public string name { get; set; }
public string state { get; set; }
public string type { get; set; }
public int inboundQueueSize { get; set; }
public int outboundQueueSize { get; set; }
}
public class Folder
{
public int id { get; set; }
public string name { get; set; }
public IList<Folder> childFolders { get; set; }
public IList<Component> childComponents { get; set; }
}
public class Data
{
public int id { get; set; }
public string name { get; set; }
public IList<Folder> childFolders { get; set; }
public IList<Component> childComponents { get; set; }
}
public class Root
{
public Data data { get; set; }
public object error { get; set; }
}
然后你可以通过递归找到所有组件:
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
Root r = parse(content); //view JSON.NET or something else
List<Component> components = r.data.childComponents.where(c=>!string.IsNullOrEmpty(c.name) && c.type == "COMMUNICATION_POINT").toList();
components.AddRange(r.data.childFolders.Select(ExtractComponentsFromFolers));
}
public static List<Component> ExtractComponentsFromFolers(Folder folser)
{
if (folser == null) return new List<Component>();
List<Component> result = folser.childComponents.where(c=>!string.IsNullOrEmpty(c.name) && c.type == "COMMUNICATION_POINT").toList();
result.AddRange(folser.childFolders.Select(ExtractComponentsFromFolers));
return result;
}
}
很可能您必须添加更多 Null 检查条件,但我认为这就是您正在寻找的。