-1

基于以下经过验证的 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 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
        }
    ]
}

我列出了上述解决方案以显示我试图解决所提出问题的结构。

4

1 回答 1

0

这样的事情怎么样?

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");

小提琴:https ://dotnetfiddle.net/KVslv2

于 2018-11-08T05:20:49.560 回答