【问题标题】:Is it possible to deserialize the results from an Azure Search?是否可以反序列化 Azure 搜索的结果?
【发布时间】:2017-09-14 09:06:27
【问题描述】:

从 REST API 调用 Azure 搜索,结果以类似 JSON 的结构返回。

{"@odata.context":"https://xxxx.search.windows.net/indexes('index-blob')/$metadata#docs(metadata_storage_size,metadata_storage_last_modified,metadata_storage_name,metadata_storage_path,metadata_content_type,metadata_title)","value":[{"@search.score":0.012103397,"metadata_storage_size":1479948,"metadata_storage_last_modified":"2017-04-17T18:31:18Z","metadata_storage_name":"90e975d1-3986-4167-87d2-4d1cdbc7be09.pdf","metadata_storage_path":"xxxx","metadata_content_type":"application/pdf","metadata_title":null},{"@search.score":0.004614377,"metadata_storage_size":116973,"metadata_storage_last_modified":"2017-04-13T18:24:01Z","metadata_storage_name":"xxx.pdf","metadata_storage_path":"xxxx","metadata_content_type":"application/pdf","metadata_title":"xxx"}]}

问题是我找不到真正反序列化它的方法。我无法弄清楚可以反序列化“@search.score”(或者如果查询更复杂,则使用类似参数)的结构。我尝试过使用各种 JSON->C# 转换器(包括 VS 中的 Edit->Paste Special),但没有任何效果。我不得不手动解析这些结果似乎很奇怪......我将其归因于我不了解 Azure 搜索或 JSON 的东西。

【问题讨论】:

  • 你能用JsonProperty属性解决这个问题吗? newtonsoft.com/json/help/html/… 例如。 [JsonProperty("@search.score")] public float SearchScore { get; set; } - 还是我不理解你的问题?
  • 谢谢,完全可以接受!我在想有某种结构或 Azure 搜索参数或其他必须工作的方式。 JsonProperty 工作得很好。

标签: c# json azure azure-cognitive-search


【解决方案1】:

如果我对您的理解正确并且问题在于解析其键名中包含特殊字符的 JSON 对象,那么您可以尝试在您的 POCO(要反序列化的 C# 类)中使用 JsonProperty 属性:

public class AzureSearchResult {
    [JsonProperty("@search.score")]
    public float SearchScore { get; set;}
    //other variables...
}

欲了解更多信息,请参阅http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm

【讨论】:

    【解决方案2】:

    如果您使用Azure Search .NET SDK,它将为您进行反序列化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多