简体   繁体   中英

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

This is my json string returned from an ElasticSearch query:

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}
      ]
    }
  }
}

How to I get a list of integers with only the values of the key property of the buckets node like this:

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

This is what I got so far:

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

You're nearly there, you just needed to Select the keys...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM