Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Wordnik API请求定义_C#_.net_Api - Fatal编程技术网

C# 使用Wordnik API请求定义

C# 使用Wordnik API请求定义,c#,.net,api,C#,.net,Api,我只在一个非常小的意义上使用API,所以我一直想尝试如何做到这一点有一段时间了。好的,这就是我到目前为止得到的,它可以工作,但它返回定义的所有内容。所以我有几个问题: 有没有一种方法可以只请求定义而不请求其他内容 我只是解析数据吗?我在Wordnik API中看到了,我可以包含XML标记……那么我可以使用XMLReader获取定义吗 现在,我们可以同时请求两个定义,以及它是否是名词/动词/etc吗 最终的目标是创建一个定义列表,我可以用它来做一些事情。任何帮助都将不胜感激。以下是我目前的代码:

我只在一个非常小的意义上使用API,所以我一直想尝试如何做到这一点有一段时间了。好的,这就是我到目前为止得到的,它可以工作,但它返回定义的所有内容。所以我有几个问题:

  • 有没有一种方法可以只请求定义而不请求其他内容
  • 我只是解析数据吗?我在Wordnik API中看到了,我可以包含XML标记……那么我可以使用XMLReader获取定义吗
  • 现在,我们可以同时请求两个定义,以及它是否是名词/动词/etc吗
  • 最终的目标是创建一个定义列表,我可以用它来做一些事情。任何帮助都将不胜感激。以下是我目前的代码:

    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"));
        }
    }
    
    谢谢, 贾斯汀

  • API文档可能会告诉您这一点
  • 是的,解析数据。如果数据以XML的形式出现,那么可以使用XMLReader对其进行解析,也可以将其加载到XMLDocument中。不过,看起来您需要的是JSON。如果是这样,您将需要一个JSON解析器。退房
  • 再次查看API文档

  • 他们的文档页面少得令人怀疑。你可能会在他们的谷歌群或他们网站上列出的其他来源中得到更好的回复。

    下面是一个获取单词定义的示例。您需要用自己的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);    
    
        }
    }
    
    公共类单词
    {
    公共字符串字{get;set;}
    公共字符串源字典{get;set;}
    公共字符串部分语音{get;set;}
    公共字符串文本{get;set;}
    }
    公共类词表
    {
    公共列表字列表{get;set;}
    }
    字符串url=”http://api.wordnik.com:80/v4/word.json/“+word+”/definitions?limit=200&includeRelated=false&sourceDictionaries=all&usecononical=false&includeTags=false&api_key=a2a73e7b926c924fad7001ca311acd55af2ff50eb4ae5”;
    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,比如Gecko)Chrome/39.0.2171.95 Safari/537.36”;
    webRequest.Referer=”http://developer.wordnik.com/docs.html";
    添加(“接受编码”、“gzip、deflate、sdch”);
    添加(“接受语言”,“en-US,en;q=0.8”);
    webRequest.Host=“api.wordnik.com”;
    HttpWebResponse webResponse=(HttpWebResponse)webRequest.GetResponse();
    字符串enc=webResponse.ContentEncoding;
    使用(Stream=webResponse.GetResponseStream())
    {
    StreamReader=新的StreamReader(stream,Encoding.UTF8);
    字符串responseString=“{\'wordList\”:“+reader.ReadToEnd()+”}”;
    if(responseString!=null)
    {
    JavaScriptSerializer ser=新的JavaScriptSerializer();
    WordList words=ser.Deserialize(responseString);
    }
    }