是否可以反序列化 Azure 搜索的结果?
Is it possible to deserialize the results from an Azure Search?
从 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# 转换器(包括 Edit->VS 中的选择性粘贴),但没有任何效果。我不得不手动解析这些结果似乎很奇怪......我将其归因于我对 Azure 搜索或 JSON.
不了解的东西
如果我对您的理解正确并且问题出在解析 JSON 键名称中包含特殊字符的对象,那么您可以尝试在 POCO (C# class 反序列化为):
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
如果您使用 Azure Search .NET SDK,它将为您进行反序列化。
从 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# 转换器(包括 Edit->VS 中的选择性粘贴),但没有任何效果。我不得不手动解析这些结果似乎很奇怪......我将其归因于我对 Azure 搜索或 JSON.
不了解的东西如果我对您的理解正确并且问题出在解析 JSON 键名称中包含特殊字符的对象,那么您可以尝试在 POCO (C# class 反序列化为):
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
如果您使用 Azure Search .NET SDK,它将为您进行反序列化。