使用 Json.net 转换 wikipedia api 响应
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
维基百科的 api(使用 JSON)假设您使用的是 JSONP。您可以从查询字符串中完全删除回调参数:
此外,您得到的结果可能无法转换为
Dictionary< /代码>。如果仔细观察,它实际上是一个数组,其中第一个对象是字符串(搜索词),第二个对象是字符串列表(结果)。
以下对我有用:
Wikipedia's api (using JSON) assumes you're using JSONP. You could just drop the callback parameter completely from your query string:
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: