C# 如何获得具有多个级别的 Json 计数



我有一个像这样的来自 API 的 json。 我正在使用 oauth2,所以我得到了令牌,然后我请求数据。 我想计算资源中的名字,知道有多少。

{
"startDate": "2019-06-23T16:07:21.205Z",
"endDate": "2019-07-24T16:07:21.205Z",
"status": "Complete",
"usages": [
{
"name": "PureCloud Edge Virtual Usage",
"resources": [
{
"name": "Edge01-VM-GNS-DemoSite01 (1f279086-a6be-4a21-ab7a-2bb1ae703fa0)",
"date": "2019-07-24T09:00:28.034Z"
},
{
"name": "329ad5ae-e3a3-4371-9684-13dcb6542e11",
"date": "2019-07-24T09:00:28.034Z"
},        
{
"name": "e5796741-bd63-4b8e-9837-4afb95bb0c09",
"date": "2019-07-24T09:00:28.034Z"
}
]
},
{
"name": "PureCloud for SmartVideo Add-On Concurrent",
"resources": [
{
"name": "jpizarro@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "jaguilera@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},        
{
"name": "dcortes@gns.com.co",
"date": "2019-07-15T15:06:09.203Z"
}
]
},
{
"name": "PureCloud 3 Concurrent User Usage",
"resources": [
{
"name": "jpizarro@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "jaguilera@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},       
{
"name": "dcortes@gns.com.co",
"date": "2019-07-15T15:06:09.203Z"
}
]
},
{
"name": "PureCloud Skype for Business WebSDK",
"resources": [
{
"name": "jpizarro@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},
{
"name": "jaguilera@gns.com.co",
"date": "2019-06-25T04:54:17.662Z"
},      
{
"name": "dcortes@gns.com.co",
"date": "2019-07-15T15:06:09.203Z"
}
]
}
],
"selfUri": "/api/v2/billing/reports/billableusage"
}

这是我使用的类。

public class Resources
{
public string name { get; set; }
public DateTime date { get; set; }
}
public class Usages
{
public string name { get; set; }
public IList<Resources> resources { get; set; }
}
public class Example
{
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public string status { get; set; }
public IList<Usages> usages { get; set; }
public string selfUri { get; set; }
}

这是我使用的函数,它接收 url 和令牌并获取数据:

static async Task<int> GetNamesCount(string theUrl, string theToken)        
{
using (var client = new HttpClient())
{
Records records = new Records();                
client.BaseAddress = new Uri(theUrl);               
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", theToken);
try
{
HttpResponseMessage response = await client.GetAsync(theUrl);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();                    
records = (Records)new DataContractJsonSerializer(typeof(Records)).ReadObject(response.Content.ReadAsStreamAsync().Result);

我试过了,但我只能数前两个级别。我已经搜索过,但我不能使用 [1] 或 records.usages["资源"]。 我能做什么? 提前谢谢。

您可以使用SelectMany获取用法列表中的所有资源名称,并使用Count方法计算资源内部名称的计数:

int countNames = records.usages.SelectMany(x => x.resources.Select(y => y.name)).Count();

或 将Sumcount一起使用,例如:

int countNames = records.usages.Sum(x => x.resources.Count(y => y.name != null));

如果你只想要Usages名称count,你可以使用:

int countUsagesNames = records.usages.Count;
// or
int countUsagesNames = records.usages.Count(x => x.name != null);

我希望对您有所帮助。

相关内容

  • 没有找到相关文章

最新更新