如何将 ElasticSearch 聚合查询的存储桶键值解析为 C# 中的整数列表

How to parse the buckets key values of an ElasticSearch Aggregations query to a list of integers in C#

这是我从 ElasticSearch 查询返回的 json 字符串:

var response = 
{
  "took":1,
  "timed_out":false,
  "_shards":
  {
    "total":5,
    "successful":5,
    "skipped":0,
    "failed":0
  },
  "hits":
  {
    "total":278,
    "max_score":0.0,
    "hits":[]
  },
  "aggregations":
  {
    "nombindiam": 
    {
      "doc_count_error_upper_bound":0,
      "sum_other_doc_count":0,
      "buckets":
      [
       {"key":15,"doc_count":12},
       {"key":20,"doc_count":24},
       {"key":25,"doc_count":44}
      ]
    }
  }
}

如何获取仅包含桶节点的 key属性 值的整数列表,如下所示:

List<int> myKeyValues= new List<int>() { 15, 20, 25 };

这是我目前得到的:

JObject json = JObject.Parse(response);
var myKeyValues = json["aggregations"]["nombindiam"]["buckets"].ToList();

你快到了,你只需要 Select 钥匙...

var myKeyValues = json["aggregations"]["nombindiam"]["buckets"]
    .Select(x => x["key"])
    .ToList();