检索相对于指定内部数组 属性 值与指定外部数组 属性 值配对的内部数组值
Retrieve an inner array value relative to a specified inner array property value paired with a specified outer array property value
基于以下经过验证的 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” years" 和 GrowthRateItem
数组 属性, ["ItemDescriptionText"]["txt"]
, value 在这种情况下也是指定值 "Total number of employees" 或 "Sales Revenue".
LINQ to 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
基于以下经过验证的 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” years" 和 GrowthRateItem
数组 属性, ["ItemDescriptionText"]["txt"]
, value 在这种情况下也是指定值 "Total number of employees" 或 "Sales Revenue".
LINQ to 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