Microsoft 认知服务 Web 搜索 API - 反序列化问题

Microsoft Cognitive Services Web Search API - DeSerialization Issues

我想学习认知服务网络搜索 API,所以我开始创建一个机器人应用程序。我已经有了一个帐户子密钥和其他所需信息,我还阅读了很多文章并观看了 build 2016 视频,因为 well.I 在反序列化结果时遇到了问题。 我找不到可以用来执行此操作的代理 class。 我使用的 url 是 https://api.cognitive.microsoft.com/bing/v5.0/search/ 我找到了 class 以前 api 版本的代理。谁能告诉我如何在 VS 2015 中为这些服务获取 api 请求/响应的代理 class。

我的代码如下所示:

  string BingSearchUrl =  "https://api.cognitive.microsoft.com/bing/v5.0/search/";
   const string bingKey = "Key";

    public static  async Task<string> Search(string query)
    {
        var client = HttpClientFactory.Create();
        var queryString = BingSearchUrl + "?q=" + query + "&count=10";        
        // Request headers
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", bingKey);
        client.DefaultRequestHeaders.Add("Accept", "application/json");

        // Request parameters
        string r = await client.GetStringAsync(queryString);
        var jsonResult = JsonConvert.DeserializeObject<Bing.ExpandableSearchResult>(r);

        return jsonResult.Web.First().Title;

试试下面的代码

  public string BingSearchUrl = "https://api.cognitive.microsoft.com/bing/v5.0/search/";
        const string bingKey =[KEY];

        public async void Search()
        {
            var client = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", bingKey);

            // Request parameters
            queryString["q"] = "microsoft";
            queryString["count"] = "10";
            queryString["offset"] = "0";
            queryString["mkt"] = "en-us";
            queryString["safeSearch"] = "Moderate";
            var uri = "https://api.cognitive.microsoft.com/bing/v5.0/news/search?" + queryString;

            var response = await client.GetStringAsync(uri);
            var jsonResult = JsonConvert.DeserializeObject<BingJson>(response);

            string title = jsonResult.value[0].name.ToString();

        }

使用 jsonResult.value[0] 您可以遍历结果。第一个结果在 [0] 位置。

我创建了一个模型 class 查看 bing 搜索响应 json。看起来,

 public class BingJson
    {
        public string _type { get; set; }
        public Instrumentation instrumentation { get; set; }
        public string readLink { get; set; }
        public int totalEstimatedMatches { get; set; }
        public Value[] value { get; set; }
    }

    public class Instrumentation
    {
        public string pingUrlBase { get; set; }
        public string pageLoadPingUrl { get; set; }
    }

    public class Value
    {
        public string name { get; set; }
        public string url { get; set; }
        public string urlPingSuffix { get; set; }
        public Image image { get; set; }
        public string description { get; set; }
        public About[] about { get; set; }
        public Provider[] provider { get; set; }
        public DateTime datePublished { get; set; }
        public string category { get; set; }
    }

    public class Image
    {
        public Thumbnail thumbnail { get; set; }
    }

    public class Thumbnail
    {
        public string contentUrl { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    public class About
    {
        public string readLink { get; set; }
        public string name { get; set; }
    }

    public class Provider
    {
        public string _type { get; set; }
        public string name { get; set; }
    }

有了这个模型,我能够得到想要的结果。其他答案中提供的模型不适用于我的情况。

namespace BingSearchBot
{
    public class RootObject
    {
        public string _type { get; set; }
        public WebPages webPages { get; set; }
        public RelatedSearches relatedSearches { get; set; }
        public RankingResponse rankingResponse { get; set; }
    }
    public class WebPages
    {
        public string webSearchUrl { get; set; }
        public int totalEstimatedMatches { get; set; }
        public List<Value> value { get; set; }
    }
    public class Value
    {
        public string id { get; set; }
        public string name { get; set; }
        public string url { get; set; }
        public List<About> about { get; set; }
        public string displayUrl { get; set; }
        public string snippet { get; set; }
        public List<DeepLink> deepLinks { get; set; }
        public string dateLastCrawled { get; set; }
        public List<SearchTag> searchTags { get; set; }
    }
    public class About
    {
        public string name { get; set; }
    }

    public class DeepLink
    {
        public string name { get; set; }
        public string url { get; set; }
        public string snippet { get; set; }
    }

    public class SearchTag
    {
        public string name { get; set; }
        public string content { get; set; }
    }
    public class Value2
    {
        public string text { get; set; }
        public string displayText { get; set; }
        public string webSearchUrl { get; set; }
    }

    public class RelatedSearches
    {
        public string id { get; set; }
        public List<Value2> value { get; set; }
    }

    public class Value3
    {
        public string id { get; set; }
    }

    public class Item
    {
        public string answerType { get; set; }
        public int resultIndex { get; set; }
        public Value3 value { get; set; }
    }

    public class Mainline
    {
        public List<Item> items { get; set; }
    }

    public class RankingResponse
    {
        public Mainline mainline { get; set; }
    }
}