0

ウィキペディアAPIを使用してデータをクエリしており、結果を文字列[]に変換したいと思います。

クエリ「テスト」

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)"]])

Json.netを使用して、タグ「spellcheck」を削除または無視できますか?このコードを使用して応答を変換すると、アプリケーションがクラッシュします。

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

1 に答える 1

4

ウィキペディアの API (JSON を使用) は、JSONP を使用していることを前提としています。クエリ文字列からコールバック パラメータを完全に削除できます。

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

さらに、得られた結果はおそらくDictionary<string, string[]>. よく見ると、実際には、最初のオブジェクトが文字列 (検索語) で、2 番目のオブジェクトが文字列のリスト (結果) である配列です。

以下は私のために働いた:

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();
    }
}
于 2011-11-12T20:52:09.327 に答える