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

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

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

问题

Here's the translated code snippet you provided:

  1. var json = "data": {
  2. "results": abc,
  3. "faresAvailable": {
  4. "thamil-": {
  5. "totals": 1,
  6. "isSumOfSector": false,
  7. "fareAvailabilityKey": "MH5UM35_UVB_VDNPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITA-",
  8. "fares": idk,
  9. },
  10. "rocky-": {
  11. "totals": 2,
  12. "isSumOfSector": false,
  13. "fareAvailabilityKey": "MH5UN35_UVB_VDdPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITE-",
  14. "fares": idk too,
  15. }
  16. },
  17. "currencyCode": "INR",
  18. "includeTaxesAndFees": true,
  19. "bundleOffers": null
  20. }
  21. }
  22. var A = from a in json["data"]
  23. select new {
  24. fareAvailabilityKey = json["faresAvailable"].Values().Select(a => a["fareAvailabilityKey"].ToString())
  25. };

And here's the desired output:

  1. fareAvailabilityKey
  2. MH5UM35_UVB_VDNPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITA-
  3. MH5UN35_UVB_VDdPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITE-

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

英文:
  1. var json= "data": {
  2. "results": abc,
  3. "faresAvailable": {
  4. "thamil-": {
  5. "totals": 1,
  6. "isSumOfSector": false,
  7. "fareAvailabilityKey": "MH5UM35_UVB_VDNPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITA-",
  8. "fares": idk,
  9. },
  10. "rocky-": {
  11. "totals": 2,
  12. "isSumOfSector": false,
  13. "fareAvailabilityKey": "MH5UN35_UVB_VDdPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITE-",
  14. "fares": idk too,
  15. }
  16. },
  17. "currencyCode": "INR",
  18. "includeTaxesAndFees": true,
  19. "bundleOffers": null
  20. }
  21. }

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

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

I want the output like:

  1. fareAvailabilityKey
  2. MH5UM35_UVB_VDNPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITA-
  3. MH5UN35_UVB_VDdPN1pCSVh_MTAwN35_MTl_MX5OQk9NTUFBMDE5MDAxMH5YITE-

some one please help..!

答案1

得分: 3

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

  1. var jsonObject = JObject.Parse(json);
  2. var fareAvailabilityKeys = jsonObject["data"]["faresAvailable"]
  3. .Children()
  4. .Select(a => (string)a["fareAvailabilityKey"]);
  5. foreach (var fareAvailabilityKey in fareAvailabilityKeys)
  6. {
  7. Console.WriteLine(fareAvailabilityKey);
  8. }
英文:

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

  1. var jsonObject = JObject.Parse(json);
  2. var fareAvailabilityKeys = jsonObject["data"]["faresAvailable"]
  3. .Children()
  4. .Select(a => (string)a["fareAvailabilityKey"]);
  5. foreach (var fareAvailabilityKey in fareAvailabilityKeys)
  6. {
  7. Console.WriteLine(fareAvailabilityKey);
  8. }

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:

确定