.NET(C#)通过JSON.NET反序列化Elasticsearch返回响应的结果

本文介绍了如何在.NET环境中,利用C#和JSON.NET库查询Elasticsearch索引并反序列化结果。具体步骤包括定义Result类映射JSON结构,使用LINQ-to-JSONAPI解析响应体,最后输出查询结果的Id,Host和PostalCode。
摘要由CSDN通过智能技术生成

简介:

本文主要介绍.NET(C#)查询Elasticsearch索引结果,通过JSON.NET反序列化结果的方法。

1、示例查询结果

{
  "took":31,
  "timed_out":false,
  "_shards": {
    "total":91,
    "successful":91,
    "skipped":0,
    "failed":0
  },
  "hits":{
    "total":1,
    "max_score":1.0,
    "hits":[
      {
        "_index":"my-index",
        "_type":"doc",
        "_id":"TrxrZGYQRaDom5XaZp23",
        "_score":1.0,
        "_source":{
          "my_id":"65a107ed-7325-342d-adab-21fec0a97858",
          "host":"something",
          "zip":"12345"
        }
      },
    ]
  }
}

2、反序化用到Result类

public class Result
{
  [JsonProperty(PropertyName = "my_id")]
  public string Id { get; set; }
  [JsonProperty(PropertyName = "host")]
  public string Host { get; set; }
  [JsonProperty(PropertyName = "zip")]
  public string PostalCode { get; set; }
}

3、使用 Json.Net的 LINQ-to-JSON API 获取指定的节点,然后将它们转换为结果列表:

var results = JToken.Parse(response.Body)
                    .SelectTokens("hits.hits[*]._source")
                    .Select(t => t.ToObject<Result>())
                    .ToList();

4、完整的代码:

using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
public class Program
{
	public static void Main()
	{
		string json = @"
            {
              ""took"": 31,
              ""timed_out"": false,
              ""_shards"": {
                ""total"": 91,
                ""successful"": 91,
                ""skipped"": 0,
                ""failed"": 0
              },
              ""hits"": {
                ""total"": 1,
                ""max_score"": 1.0,
                ""hits"": [
                  {
                    ""_index"": ""my-index"",
                    ""_type"": ""doc"",
                    ""_id"": ""TrxrZGYQRaDom5XaZp23"",
                    ""_score"": 1.0,
                    ""_source"": {
                      ""my_id"": ""65a107ed-7325-342d-adab-21fec0a97858"",
                      ""host"": ""something"",
                      ""zip"": ""12345""
                    }
                  },
                  {
                    ""_index"": ""my-index"",
                    ""_type"": ""doc"",
                    ""_id"": ""jhgDtrEYHrjgJafgRy456"",
                    ""_score"": 0.95,
                    ""_source"": {
                      ""my_id"": ""52f0be23-1d2f-4779-bde9-32fc9e50168e"",
                      ""host"": ""something else"",
                      ""zip"": ""06789""
                    }
                  }
                ]
              }
            }";
		var results = JToken.Parse(json)
			                .SelectTokens("hits.hits[*]._source") 
			                .Select(t => t.ToObject<Result>())
			                .ToList();
		foreach (var result in results)
		{
			Console.WriteLine("Id: " + result.Id);
			Console.WriteLine("Host: " + result.Host);
			Console.WriteLine("PostalCode: " + result.PostalCode);
			Console.WriteLine();
		}
	}
}
public class Result
{
	[JsonProperty(PropertyName = "my_id")]
	public string Id { get; set; }
	[JsonProperty(PropertyName = "host")]
	public string Host { get; set; }
	[JsonProperty(PropertyName = "zip")]
	public string PostalCode { get; set; }
}

了解更多分析及数据抓取可查看:
http://data.yisurvey.com:8989/
特别说明:本文旨在技术交流,请勿将涉及的技术用于非法用途,否则一切后果自负。如果您觉得我们侵犯了您的合法权益,请联系我们予以处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值