使用JSONPath还是SelectTokens查询JSON?使用C#中的JSON.NET

3

我希望你能帮我翻译一下关于c#中使用Newtonsoft.Json.Net的内容。以下是我需要从JSON文件中检索数据的部分:

{
    "video":{
        "local_recording_device":{
            "codecs":null
        },
        "preferred_string":"___PREFERRED___",
        "streams":{
            "99176901":{
                "id":"99176901",
                "name":"PTZ Camera",
                "site":"someone",
                "email":"someone@awebsite.com",
                "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":"joe@awebsite.com",
                "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":"asitesame@awebsite.com",
                "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数据内部有: "video","streams",在"streams"内可能有x个不同的流(流ID)。 "streams"中的流(长数字)可以随时更改。在我的示例中,有三个。我需要搜索“streams”中的所有流,并查看其中是否有与特定电子邮件地址匹配的“电子邮件”。每个流都有一个“电子邮件”。如果电子邮件与提供的电子邮件地址匹配,则需要检查该流的“启用”状态,以查看它是真还是假。
感谢您的帮助,引导我朝着正确的方向发展。我以前没有使用过JSON数据。

1
你能展示一下你正在处理的JSON数据和期望的输出吗? - Andrew Whitaker
1个回答

7

您可以使用 LINQ to JSONSelectTokens 进行所需的查询:

var testEmail = "someone@awebsite.com"; // The email for which we are searching

var obj = JObject.Parse(jsonString);
var streamQuery = obj.SelectTokens("video.streams.*")
    .Where(s => (string)s["email"] == testEmail);
var firstEnabled = streamQuery
    .Select(s => (bool?)s["enable"])
    .FirstOrDefault();

查询返回一个可空的bool,如果所需电子邮件的第一个流已启用,则为truefalse,如果没有该电子邮件地址的流,则为null

请注意,这将返回与给定电子邮件地址匹配的第一个流的enabled状态。如果您想知道是否启用了任何流,请执行以下操作:

var anyEnabled = streamQuery.Any(s => (bool)s["enable"]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接