這個問題是非常相關的以下內容的JSON對象查詢JSON SelectTokens對於具有結構變化
Querying JSON with JSONPath or SelectTokens? With JSON.NET in C#
到上面的問題一樣,如何才能讓Where
查詢時的JSON對象並不總是具有相同的簡化的數據結構,即
{
"video": {
"local_recording_device": {
"codecs": null
},
"preferred_string": "___PREFERRED___",
"streams": {
"22855218": {
"id": "22855218",
"name": "AJA Camera"
},
"99176901": {
"id": "99176901",
"name": "PTZ Camera",
"site": "someone",
"email": "[email protected]",
"codec": [
"VP8",
"HD1",
"(720p)"
]
},
"3091494011": {
"id": "3091494011",
"name": "Logitech Webcam C930e",
"site": "Joe Smith",
"email": "[email protected]",
"codec": [
"VP8",
"Medium",
"(CIF)"
]
},
"3798287599": {
"id": "3798287599",
"name": "Drive Camera",
"site": "ASiteName",
"email": "[email protected]",
"codec": [
"HD1",
"(720p)"
]
}
}
}
}
我怎樣才能獲得Where
查詢,以便對給定的編解碼器,如工作「VP8」?
它似乎沒有,如果我查詢它像這樣與我的應用程序一起工作:
string json = GetJson();
var obj = JObject.Parse(json);
var testcodec = "VP8";
var streamQuery = obj.SelectTokens("video.streams.*.codec")
.Where(s =>(string)s == testcodec);
但我的結果應該是這樣的:
{
"video":{
"local_recording_device":{
"codecs":null
},
"preferred_string":"___PREFERRED___",
"streams":{
"99176901":{
"id":"99176901",
"name":"PTZ Camera",
"site":"someone",
"email":"[email protected]",
"codec":[
"VP8"
]
},
"3091494011":{
"id":"3091494011",
"name":"Logitech Webcam C930e",
"site":"Joe Smith",
"email":"[email protected]",
"codec":[
"VP8"
]
}
}
}
}
究竟你希望得到和你實際得到什麼? –
我想獲得一個類似於原始結構的數組,但所有元素都滿足我的查詢要求。在這種情況下,具有以下兩個流的完整json樹3091494011,999176901 – Andres