基于以下经过验证的JSON:
{
"GrowthRates": [{
"FinancialStatementHeader": {
"TimePeriodText": {
"attrCodeValue": 11096,
"txt": "Between 0 and 12 months"
}
},
"ComparedToFinancialStatementHeader": {
"TimePeriodText": {
"attrCodeValue": 13711,
"txt": "1-3 years"
}
},
"GrowthRateItem": [{
"ItemDescriptionText": {
"attrCodeValue": 6240,
"txt": "Total number of employees"
},
"ItemRate": 0
},
{
"ItemDescriptionText": {
"attrCodeValue": 3110,
"txt": "Sales Revenue"
},
"ItemRate": 0
}
]
},
{
"FinancialStatementHeader": {
"TimePeriodText": {
"attrCodeValue": 11096,
"txt": "Between 0 and 12 months"
}
},
"ComparedToFinancialStatementHeader": {
"TimePeriodText": {
"attrCodeValue": 13721,
"txt": "1-5 years"
}
},
"GrowthRateItem": [{
"ItemDescriptionText": {
"attrCodeValue": 6240,
"txt": "Total number of employees"
},
"ItemRate": 0
},
{
"ItemDescriptionText": {
"attrCodeValue": 3110,
"txt": "Sales Revenue"
},
"ItemRate": 0
}
]
}
]
}
我需要将ItemRate值检索为字符串,其中GrowthRates数组属性["ComparedToFinancialStatementHeader"]["TimePeriodText"]["txt"]
的值在本例中为"1-3年"或"1-5年"的指定值,而GrowthRateItem
阵列属性["ItemDescriptionText"]["txt"]
的值在该例中也是"员工总数"或"销售收入"的指定值。
LINQ到JSON查询只需要实现上面列出的四个值所隐含的四种情况中的一种情况。
我无法扩展以下用于解决类似问题的解决方案。
LINQ查询,根据外部数组中的两个属性值检索内部数组值。
JToken jsonTkn = JToken.Parse(jsonString);
string jsonValue = jsonTkn["IndustryCode"]
.Where(icItem => icItem["attrTypeText"].Value<string>() == "NAICS" && icItem["DisplaySequence"].Value<string>() == "1")
.Select(icItem => icItem["IndustryCodeDescription"].First()["txt"].Value<string>())
.First().ToString().ToUpper();
JSON字符串:
{
"IndustryCode": [{
"attrCodeValue": 25838,
"attrTypeText": "DBH Industry Code",
"IndustryCode": {
"txt": "1063"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 9120,
"txt": "Legal Services"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 700,
"attrTypeText": "NAICS",
"IndustryCode": {
"txt": "541110"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 9120,
"txt": "Offices of Lawyers"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 24664,
"attrTypeText": "North American Industry Classification System 2012",
"IndustryCode": {
"txt": "541110"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 9120,
"txt": "Offices of Lawyers"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 21182,
"attrTypeText": "UK SIC 2003",
"IndustryCode": {
"txt": "74.110"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 9120,
"txt": "Legal activities"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 3599,
"attrTypeText": "DBS Industry Code",
"IndustryCode": {
"txt": "81119901"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 2121,
"txt": "GENRL PRACTICE ATTY"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 399,
"attrTypeText": "US Standard Industry Code 1987 - 4 digit",
"IndustryCode": {
"txt": "8111"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 1441,
"txt": "Legal services office"
}],
"DisplaySequence": 1
}
]
}
我列出了上面的解决方案,以显示我试图解决所提出问题的结构。
这样的东西怎么样?
public static string GetItemRate(JObject obj, string timePeriodText, string itemDescriptionText)
{
return obj.SelectToken("GrowthRates")
.First(jo => (string)jo.SelectToken("ComparedToFinancialStatementHeader.TimePeriodText.txt") == timePeriodText)
.SelectToken("GrowthRateItem")
.First(jo => (string)jo.SelectToken("ItemDescriptionText.txt") == itemDescriptionText)
.SelectToken("ItemRate")
.Value<string>();
}
然后这样使用:
JObject obj = JObject.Parse(jsonString);
string rate = GetItemRate(obj, "1-3 years", "Sales Revenue");
Fiddle:https://dotnetfiddle.net/KVslv2