问题描述
我正在尝试在C#中使用Newtonsoft.Json.Net.以下是我需要从中检索数据的JSON文件的一部分:
I am trying to use the Newtonsoft.Json.Net in c#. The following is part of JSON file that I need to retrieve data out of:
{
"video":{
"local_recording_device":{
"codecs":null
},
"preferred_string":"___PREFERRED___",
"streams":{
"99176901":{
"id":"99176901",
"name":"PTZ Camera",
"site":"someone",
"email":"[email protected]",
"codec":"VP8 HD1 (720p)",
"local":true,
"screen":false,
"fit_to_window":true,
"stay_on_top":false,
"layout":0,
"native_width":1280,
"native_height":720,
"window_width":456,
"window_height":254,
"preferred":false,
"local_recording":false,
"device_id":"MJPEG Camera",
"normalized_device_id":"MJPEGCamera",
"shared_window_id":"MJPEG Camera",
"enable":true,
"big_location":"2",
"x":347,
"y":737,
"window_id":"197302",
"camera_id":null
},
"3091494011":{
"id":"3091494011",
"name":"Logitech Webcam C930e",
"site":"Joe Smith",
"email":"[email protected]",
"codec":"VP8 Medium (CIF)",
"local":false,
"screen":false,
"fit_to_window":true,
"stay_on_top":false,
"layout":0,
"native_width":352,
"native_height":288,
"window_width":864,
"window_height":702,
"preferred":true,
"local_recording":false,
"enable":true,
"big_location":"1",
"x":204,
"y":0,
"window_id":"197296",
"camera_id":null
},
"3798287599":{
"id":"3798287599",
"name":"Drive Camera",
"site":"ASiteName",
"email":"[email protected]",
"codec":"VP8 HD1 (720p)",
"local":true,
"screen":false,
"fit_to_window":true,
"stay_on_top":false,
"layout":0,
"native_width":1280,
"native_height":720,
"window_width":456,
"window_height":254,
"preferred":true,
"local_recording":false,
"device_id":"Logitech Webcam C930e",
"normalized_device_id":"LogitechWebcamC930e",
"shared_window_id":"Logitech Webcam C930e",
"enable":true,
"big_location":"3",
"x":814,
"y":737,
"window_id":"262822",
"camera_id":null
}
}
}
}
因此,在JSON数据内部是:流中的视频",流"可以是x个不同的流(流ID). 流"(长号)中的流可以随时更改.在我的示例中,这里有三个.我需要搜索信息流"中的所有信息流,看看其中是否有匹配特定电子邮件地址的电子邮件".每个流都有一个电子邮件".如果电子邮件与我提供的电子邮件地址匹配,我需要检查流是否启用"以查看它是真还是假.
So, inside the JSON data is: "video", "streams" inside streams can be x amount of different streams (stream id's). The streams in "streams" (the long numbers) can change at anytime. In my example here there are three. I need to search through all streams in "streams" and see if any of them has a "email" that matches a particular email address. Each of the streams has a "email". If a email matches my supplied email address I need to check that streams "enable" to see if it's true or false.
在向正确的方向指引我方面提供的任何帮助,我们将不胜感激.我以前没有使用过JSON数据.
Any help is appreciated in leading me in the right direction. I have not worked with a JSON data before.
推荐答案
您可以使用 LINQ to JSON 和 SelectTokens
进行所需的查询:
You can use LINQ to JSON and SelectTokens
to do the required query:
string json = GetJson();
var obj = JObject.Parse(json);
var testEmail = "[email protected]";
var streamQuery = obj.SelectTokens("video.streams.*").Where(s => (string)s["email"] == testEmail);
var firstEnabled = streamQuery.Select(s => (bool?)s["enable"]).FirstOrDefault();
查询返回可为空的bool
,即为true
或false
如果启用了所需电子邮件的第一个流,或者null
如果该电子邮件地址没有流.
The query returns a nullable bool
that is true
or false
if the first stream for the desired email is enabled, or null
if there is no stream for that email address.
请注意,这将返回与给定电子邮件地址匹配的 first 流的enabled
状态.如果您想知道是否启用了任何,请执行以下操作:
Note that this returns the enabled
state of the first stream matching the given email address. If you want to know if any are enabled, do:
var anyEnabled = streamQuery.Any(s => (bool)s["enable"]);
这篇关于使用JSONPath或SelectTokens查询JSON?在C#中使用JSON.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!