使用 Json.net 转换 wikipedia api 响应

发布于 2024-12-15 07:46:52 字数 647 浏览 2 评论 0原文

我正在使用 wikipedia api 查询数据,并希望将结果转换为 string[]。

查询“test”

en.wikipedia.org/w/api.php?action=opensearch&search=test&format=json&callback=spellcheck

在此处返回此结果:

spellcheck(["test",["Test cricket","Test","Testicle","Testudines","Testosterone","Test pilot","Test (assessment)","Testimonial match","Testimony","Testament (band)"]])

Can I use Json.net to drop orignor the tag “spellcheck”? 如果我使用此代码转换响应,应用程序将崩溃:

Dictionary<string, string[]> dict = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(response); 

I'm querying data using wikipedia api and would like to convert the result into a string[].

The query "test"

en.wikipedia.org/w/api.php?action=opensearch&search=test&format=json&callback=spellcheck

returns this result here:

spellcheck(["test",["Test cricket","Test","Testicle","Testudines","Testosterone","Test pilot","Test (assessment)","Testimonial match","Testimony","Testament (band)"]])

Can I use Json.net to drop or ignore the tag "spellcheck"?
If I convert the response using this code, the application crashes:

Dictionary<string, string[]> dict = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(response); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦 2024-12-22 07:46:53

维基百科的 api(使用 JSON)假设您使用的是 JSONP。您可以从查询字符串中完全删除回调参数:

en.wikipedia.org/w/api.php?action=opensearch&search=test&format=json

此外,您得到的结果可能无法转换为 Dictionary< /代码>。如果仔细观察,它实际上是一个数组,其中第一个对象是字符串(搜索词),第二个对象是字符串列表(结果)。

以下对我有用:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
    @"http://en.wikipedia.org/w/api.php?action=opensearch&search=test&format=json");

string[] searchResults = null;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        JArray objects = JsonConvert.DeserializeObject<JArray>(reader.ReadToEnd());
        searchResults = objects[1].Select(j => j.Value<string>()).ToArray();
    }
}

Wikipedia's api (using JSON) assumes you're using JSONP. You could just drop the callback parameter completely from your query string:

en.wikipedia.org/w/api.php?action=opensearch&search=test&format=json

Additionally, the result you're getting probably cannot be converted into a Dictionary<string, string[]>. If you look closely, it's actually an array where the first object is a string (search term) and the second is a list of strings (results).

The following worked for me:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
    @"http://en.wikipedia.org/w/api.php?action=opensearch&search=test&format=json");

string[] searchResults = null;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        JArray objects = JsonConvert.DeserializeObject<JArray>(reader.ReadToEnd());
        searchResults = objects[1].Select(j => j.Value<string>()).ToArray();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文