使用 JSONPath 或 SelectTokens 查询 JSON?在 C# 中使用 JSON.NET

Querying JSON with JSONPath or SelectTokens? With JSON.NET in C#

我正在尝试在 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" 内的流可以是 x 数量的不同流(流 ID)。 "streams" 中的流(长数字)可以随时更改。在我这里的例子中有三个。我需要搜索 "streams" 中的所有流,看看是否有一个 "email" 与特定的电子邮件地址相匹配。每个流都有一个 "email"。如果电子邮件与我提供的电子邮件地址匹配,我需要检查流 "enable" 以查看它是对还是错。

如果能帮助我朝着正确的方向前进,我们将不胜感激。我之前没有使用过 JSON 数据。

您可以使用 LINQ to JSON and SelectTokens 进行所需的查询:

        string json = GetJson();
        var obj = JObject.Parse(json);
        var testEmail = "someone@awebsite.com";

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

查询 returns a nullable booltruefalse 如果启用了所需电子邮件的第一个流,或者 null 如果有该电子邮件地址没有流。

请注意,此 returns 第一个 流的 enabled 状态与给定的电子邮件地址匹配。如果您想知道是否启用了 any,请执行:

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