英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论