我只在最低限度的意义上使用过 API,所以一段时间以来我一直想尝试如何做到这一点。好的,这就是我到目前为止所拥有的并且它有效,但它返回定义的所有内容。所以我有几个问题:
- 有没有办法只请求定义而无需其他任何内容?
- 我只是解析数据吗?我在 Wordnik API 中看到,我可以包含 XML 标签...那么我可以使用 XMLReader 来获取定义吗?
- 现在立即请求定义以及它是否是名词/动词/等怎么样?
最终目标是创建一个我可以使用的定义列表。任何帮助将不胜感激。到目前为止,这是我的代码:
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 key 替换为您自己的 api key 。
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);
}
}
关于c# - 使用 Wordnik API 请求定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18124896/