我试图在这个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的解决方案也会有所帮助。 所以现在如果我有一个名称的组件,我必须找到该组件的状态。组件的类型将是&#34; COMMUNICATION_POINT&#34; 。 非常感谢你提前。
答案 0 :(得分:0)
首先,您的课程应如下所示:
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检查条件,但我认为这是你想要的。