使用Wordnik API请求定义

时间:2013-08-08 11:30:29

标签: c# .net api

我只使用了极少数意义上的API,所以我一直想尝试一下这样做。好的,这就是我到目前为止所做的工作,但它可以返回定义的所有内容。所以我有几个问题:

  1. 有没有其他方法可以只请求定义?
  2. 我只是解析数据吗?我在Wordnik API中看到了我可以包含XML标签......所以我可以使用XMLReader来获取定义吗?
  3. 现在如何同时请求这两个定义以及它是名词/动词/等?
  4. 最终目标是创建一个我可以做的定义列表。任何帮助将不胜感激。到目前为止,这是我的代码:

    class Program
    {
    
        static void Main(string[] args)
        {
            string apiKey = "***************";
            string wordToSearch = "";
    
            do
             {
                Console.Write("Please type a word to get the definition: ");
                wordToSearch = Console.ReadLine();
    
                if (!wordToSearch.Equals("q"))
                {
                    string url = "http://api.wordnik.com/v4/word.json/" + wordToSearch + "/definitions?api_key=" + apiKey;
                    WebRequest request = WebRequest.Create(url);
                    request.Method = "GET";
                    request.ContentType = "application/json";
    
                    using (WebResponse response = request.GetResponse())
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            StreamReader reader = new StreamReader(stream);
                            string responseFromWordnik = reader.ReadToEnd();
                            Console.WriteLine(responseFromWordnik);
                        }
                    }
                }
    
            } while (!wordToSearch.Equals("q"));
        }
    }
    

    感谢, 贾斯汀

2 个答案:

答案 0 :(得分:1)

  1. API文档可能会告诉您。
  2. 是的,解析数据。如果数据以XML形式出现,那么您可以使用XMLReader解析它,或者可以将其加载到XMLDocument中。不过,看起来你要求的是JSON。如果是这样,您将需要一个JSON解析器。查看Json.Net
  3. 再次查看API文档。
  4. 他们的文档页面非常稀疏。您可能会对其Google小组或其support page上列出的其他来源之一做出更好的回复。

答案 1 :(得分:1)

以下是获取单词定义的示例。您需要使用自己的api密钥替换api密钥。

public class Word
{
    public string word { get; set; }
    public string sourceDictionary { get; set; }
    public string partOfSpeech { get; set; }
    public string text { get; set; }
}

public class WordList
{
    public List<Word> wordList { get; set; }
}


string url = "http://api.wordnik.com:80/v4/word.json/" + word + "/definitions?limit=200&includeRelated=false&sourceDictionaries=all&useCanonical=false&includeTags=false&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5";

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Accept = "application/json";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
webRequest.Referer = "http://developer.wordnik.com/docs.html";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
webRequest.Host = "api.wordnik.com";

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

string enc = webResponse.ContentEncoding;
using (Stream stream = webResponse.GetResponseStream())
{
    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
    String responseString = "{\"wordList\":" + reader.ReadToEnd() + "}";

    if (responseString != null)
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        WordList words = ser.Deserialize<WordList>(responseString);    

    }
}