Newtonsoft JArray - 从节点指定的数组中选择多个值

Posted

技术标签:

【中文标题】Newtonsoft JArray - 从节点指定的数组中选择多个值【英文标题】:Newtonsoft JArray - Select Multiple Values from Array specificed by node 【发布时间】:2020-10-22 08:15:16 【问题描述】:

我想通过skuPartNumber 选择值servicePlanNameprovisioningStatus

例如,如果我指定 "skuPartNumber": "POWER_BI_STANDARD" 我应该得到以下内容

EXCHANGE_S_FOUNDATION
PendingProvisioning

BI_AZURE_P0
Success

我参考了 Newtonsoft 文档 here 和 here 和堆栈溢出here

这是 JSON


    "@odata.context": "",
    "value": [
        
            "id": "00-00-0",
            "skuId": "0-000-0000-00-00000",
            "skuPartNumber": "POWER_BI_STANDARD",
            "servicePlans": [
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "EXCHANGE_S_FOUNDATION",
                    "provisioningStatus": "PendingProvisioning",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "BI_AZURE_P0",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                
            ]
        ,
        
            "id": "000-0000",
            "skuId": "0-000-00-00-000",
            "skuPartNumber": "STANDARDPACK",
            "servicePlans": [
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "DYN365_CDS_O365_P1",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "KAIZALA_O365_P2",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "MICROSOFT_SEARCH",
                    "provisioningStatus": "PendingProvisioning",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "WHITEBOARD_PLAN1",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "MYANALYTICS_P2",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "OFFICEMOBILE_SUBSCRIPTION",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "BPOS_S_TODO_1",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "FORMS_PLAN_E1",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "STREAM_O365_E1",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "Deskless",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "FLOW_O365_P1",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "POWERAPPS_O365_P1",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "TEAMS1",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "SHAREPOINTWAC",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "PROJECTWORKMANAGEMENT",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "SWAY",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "INTUNE_O365",
                    "provisioningStatus": "PendingActivation",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "YAMMER_ENTERPRISE",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "MCOSTANDARD",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "SHAREPOINTSTANDARD",
                    "provisioningStatus": "Disabled",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "EXCHANGE_S_STANDARD",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                
            ]
        ,
        
            "id": "000000000000000-0000000-000",
            "skuId": "00000000000-0000-0000-0000-00000000000",
            "skuPartNumber": "PROJECTPREMIUM",
            "servicePlans": [
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "PROJECT_PROFESSIONAL",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "FLOW_FOR_PROJECT",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "DYN365_CDS_PROJECT",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "EXCHANGE_S_FOUNDATION",
                    "provisioningStatus": "PendingProvisioning",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "SHAREPOINTWAC",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "SHAREPOINT_PROJECT",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "SHAREPOINTENTERPRISE",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                ,
                
                    "servicePlanId": "0000000-0000000-00000-0000-0000000",
                    "servicePlanName": "PROJECT_CLIENT_SUBSCRIPTION",
                    "provisioningStatus": "Success",
                    "appliesTo": "Company"
                
            ]
        
    ]


我已经尝试了以下

   public static async Task<string[]> GetUserApplicationSubscriptionsForSpecificLicence(HttpClient client, string user)
        
            var payload = await client.GetStringAsync($"https://graph.microsoft.com/v1.0/users/user/licenseDetails");
            var obj = JsonConvert.DeserializeObject<JObject>(payload);
   
            var groupDescription = from g in obj["value"].Children()
                                   where g["skuPartNumber"].Contains("STANDARDPACK")
                                   select g["servicePlans"].Children().Value<string>();

            return groupDescription.ToArray();
        
public static async Task<string[]> GetUserApplicationSubscriptionsForSpecificLicence2(HttpClient client, string user)
        
            var payload = await client.GetStringAsync($"https://graph.microsoft.com/v1.0/users/user/licenseDetails");
            var obj = JsonConvert.DeserializeObject<JObject>(payload);
    
            IList<string> storeNames = obj.SelectToken("$..value[?(@.skuPartNumber== 'PROJECTPREMIUM')]").Select(s => (string)s).ToList();
     
            return storeNames.ToArray();
        

我无法理解它。

如果有人可以提供帮助,那就太好了!

【问题讨论】:

【参考方案1】:

使用 LINQ,您可以执行以下操作:

var result = obj["value"]
        .Children()
        .Where(c => c["skuPartNumber"].Value<string>() == "POWER_BI_STANDARD")
        .Select(c => c["servicePlans"])
        .Where(i => i != null)
        .SelectMany(i => i.Children())
        .Select(c => (planName: c["servicePlanName"], status: c["provisioningStatus"]))
        .ToList();

这将为您的 2 个元组的 json 集合返回:

planName                status  
EXCHANGE_S_FOUNDATION   PendingProvisioning 
BI_AZURE_P0             Success 

或者如果你只想要字符串,你可以这样做:

var strings = obj["value"]
        .Children()
        .Where(c => c["skuPartNumber"].Value<string>() == "POWER_BI_STANDARD")
        .Select(c => c["servicePlans"])
        .Where(i => i != null)
        .SelectMany(i => i.Children())
        .SelectMany(c => new[]  c["servicePlanName"].Value<string>(), c["provisioningStatus"].Value<string>())
        .ToArray();

使用json路径:

    var strings = obj.SelectTokens("$.value[?(@.skuPartNumber == 'POWER_BI_STANDARD')].servicePlans[*].['servicePlanName','provisioningStatus']")
        .Select(t => t.Value<string>())
        .ToList()

【讨论】:

非常感谢您提供的各种答案!这是完美的 @Armz 很乐意提供帮助。如果答案对您有用,请将其标记为已接受(答案左侧的绿色勾边复选标记);)

以上是关于Newtonsoft JArray - 从节点指定的数组中选择多个值的主要内容,如果未能解决你的问题,请参考以下文章

Newtonsoft JSON - 在JArray中创建JArray

将 Newtonsoft.Json.Linq.JArray 转换为特定对象类型的列表

无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“Newtonsoft.Json.Linq.JArray”

从jarray中删除指定元素的问题

如何”Newtonsoft.Json.Linq.JValue”的对象强制转换为类型“Newtonsoft.Json.Linq.JArray"?

c# 读取json的问题,JObject不能强转成JArray