我只使用了极少数意义上的API,所以我一直想尝试一下这样做。好的,这就是我到目前为止所做的工作,但它可以返回定义的所有内容。所以我有几个问题:
最终目标是创建一个我可以做的定义列表。任何帮助将不胜感激。到目前为止,这是我的代码:
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"));
}
}
感谢, 贾斯汀
答案 0 :(得分:1)
他们的文档页面非常稀疏。您可能会对其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);
}
}