C#如何获得具有多个级别的Json计数
Posted
技术标签:
【中文标题】C#如何获得具有多个级别的Json计数【英文标题】:C# how can I get a count of Json with several levels 【发布时间】:2020-09-14 18:56:29 【问题描述】:我有一个像这样的来自 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["resources"]。 我能做些什么? 提前致谢。
【问题讨论】:
您要计算records
对象中资源的名称数吗?
您要统计哪些name
参数? usages[*].name
名称、usages[*].resources[*].name
名称,还是两者兼而有之?当 JSON 中缺少特定资源名称时,您还想做什么?这是一个值得关注的问题吗?
第一个,usages[*].name
【参考方案1】:
您可以使用SelectMany 获取usages 列表 中的所有资源名称,并使用Count 方法计算资源中名称的数量:
int countNames = records.usages.SelectMany(x => x.resources.Select(y => y.name)).Count();
或者
将Sum 与count
一起使用,例如:
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);
希望对您有所帮助。
【讨论】:
Cambié las clases por unas generadas automáticamente y use tu consejo... funciona。格拉西亚斯以上是关于C#如何获得具有多个级别的Json计数的主要内容,如果未能解决你的问题,请参考以下文章