如何使用LINQ从嵌套的JSON数据中提取特定值?

huangapple go评论65阅读模式
英文:

How to Extract a Specific Value from Nested JSON Data using LINQ?

问题

Here's the translated code snippet you provided:

var json = "data": {
    "results": abc,
    "faresAvailable": {
      "thamil-": {
        "totals": 1,
        "isSumOfSector": false,
        "fareAvailabilityKey": "MH5UM35_UVB_VDNPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITA-",
        "fares": idk,
      },
      "rocky-": {
        "totals": 2,
        "isSumOfSector": false,
        "fareAvailabilityKey": "MH5UN35_UVB_VDdPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITE-",
        "fares": idk too,
      }
    },
    "currencyCode": "INR",
    "includeTaxesAndFees": true,
    "bundleOffers": null
  }
}

var A = from a in json["data"]
        select new {
          fareAvailabilityKey = json["faresAvailable"].Values().Select(a => a["fareAvailabilityKey"].ToString())
        };

And here's the desired output:

fareAvailabilityKey    
MH5UM35_UVB_VDNPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITA-
MH5UN35_UVB_VDdPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITE-

I've translated the code without providing answers to the translation question, as requested.

英文:
var json= "data": {
    "results": abc,
    "faresAvailable": {
      "thamil-": {
        "totals": 1,
        "isSumOfSector": false,
        "fareAvailabilityKey": "MH5UM35_UVB_VDNPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITA-",
        "fares": idk,
      },
      "rocky-": {
        "totals": 2,
        "isSumOfSector": false,
        "fareAvailabilityKey": "MH5UN35_UVB_VDdPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITE-",
        "fares": idk too,
      }
    },
    "currencyCode": "INR",
    "includeTaxesAndFees": true,
    "bundleOffers": null
  }
}

I have a json file like this
and I am using this query

var A=from a in json[data]
select new {
  fareAvailabilityKey = json["faresAvailable"].Values().Select(a=>a["fareAvailabilityKey"].ToString())
};

I want the output like:

fareAvailabilityKey    
MH5UM35_UVB_VDNPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITA-
MH5UN35_UVB_VDdPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITE-

some one please help..!

答案1

得分: 3

这是使用 JObject 获取所需输出的一种方式:

var jsonObject = JObject.Parse(json);
var fareAvailabilityKeys = jsonObject["data"]["faresAvailable"]
    .Children()
    .Select(a => (string)a["fareAvailabilityKey"]);

foreach (var fareAvailabilityKey in fareAvailabilityKeys)
{
    Console.WriteLine(fareAvailabilityKey);
}
英文:

Here is the one of the way to get desired output using JObject

var jsonObject = JObject.Parse(json);
var fareAvailabilityKeys = jsonObject["data"]["faresAvailable"]
    .Children()
    .Select(a => (string)a["fareAvailabilityKey"]);

foreach (var fareAvailabilityKey in fareAvailabilityKeys)
{
    Console.WriteLine(fareAvailabilityKey);
}

huangapple
  • 本文由 发表于 2023年5月22日 19:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定