C# 如何计算 Json 的多个级别

C# how can I get a count of Json with several levels

我有一个 json 像这个一样来自 api。 我使用 oauth2 所以我得到了令牌,然后我请求数据。 我想统计resources里面的名字,有多少。

{
  "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"]。 我能做什么? 提前致谢。

您可以使用 SelectMany to fetch all resources name inside usages list, and use 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 中的 namescount,您可以使用:

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

希望对您有所帮助。