Home > OS >  search in JObject that contains more objects
search in JObject that contains more objects

Time:09-29

I habe this json structur in an JObject:

{
  "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"
      }
    }
  }
}

I want to get the value of path from the object that contains the key 945360 in apps. In this example I want to get D:\\Programme\\Games\\Steam.

Currently, I have this kind of code:

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

As you can see, the code doesn't make much sense because I have realy no clue on how to the path I want.

Any idea on how to write this linq to get to the path?

CodePudding user response:

try this

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"];
  • Related