linq 在包含更多对象的JObject中搜索

ubof19bj  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(124)

我在一个JObject中有一个json结构:

{
  "libraryfolders": {
    "0": {
      "path": "C:\\Program Files (x86)\\Steam",
      "label": "",
      "contentid": "XXX",
      "totalsize": "0",
      "update_clean_bytes_tally": "120348785641",
      "time_last_update_corruption": "0",
      "apps": {
        "480": "1906055",
        "228980": "756724600",
        "238960": "32211126344",
        "311690": "333868579",
        "590830": "7120215854",
        "632360": "2247286363",
        "674940": "392892776",
        "1245620": "52346645367",
        "1521580": "5053884732",
        "1794680": "267174711",
        "1966900": "127661106"
      }
    },
    "1": {
      "path": "D:\\Programme\\Games\\Steam",
      "label": "",
      "contentid": "XXX",
      "totalsize": "2000262524928",
      "update_clean_bytes_tally": "23979958573",
      "time_last_update_corruption": "0",
      "apps": {
        "240": "4630144691",
        "730": "31689957761",
        "4000": "4052006918",
        "9420": "9134146466",
        "32470": "3794961923",
        "105600": "465676180",
        "107410": "43462897342",
        "236110": "14603360385",
        "266840": "2667618758",
        "281990": "16030502616",
        "286160": "2969544733",
        "359550": "81031709539",
        "427520": "1853059156",
        "431240": "4298680977",
        "571740": "11519231643",
        "601150": "36376736139",
        "782330": "95565268921",
        "945360": "437077095",
        "976730": "113286721086",
        "1139900": "36627541559",
        "1255980": "5724835664",
        "1281930": "110367305"
      }
    },
    "2": {
      "path": "E:\\SteamLibrary",
      "label": "",
      "contentid": "XXXX",
      "totalsize": "239410868224",
      "update_clean_bytes_tally": "16522976752",
      "time_last_update_corruption": "0",
      "apps": {
        "221640": "22708311",
        "294100": "408933883",
        "960090": "1394782344",
        "1271700": "28857297537",
        "1296610": "328347992",
        "1316230": "1238740415",
        "1621690": "250978974",
        "1818750": "6510445900"
      }
    }
  }
}

我想从包含apps中的键945360的对象中获取path的值。在本例中,我想获取D:\\Programme\\Games\\Steam
目前,我有这样的代码:

JObject test = new JObject(data);
 var path = test["libraryfolders"].Where(x => (new JObject(x))["apps"].FirstOrDefault(y => (string)y == "945360").ToString() != "").FirstOrDefault()["Path"];

正如您所看到的,代码没有多大意义,因为我真的不知道如何得到我想要的path
如何编写此链接以获得路径?

7rtdyuoh

7rtdyuoh1#

试试这个

JObject libraryfolders=(JObject) JObject.Parse(json)["libraryfolders"];

string path =  (string) libraryfolders.Properties()
                        .Where(x =>  ((JObject) x.Value["apps"]).Properties()
                        .Any(y => y.Name== "945360")).FirstOrDefault().Value["path"];

相关问题