2016-02-26 28 views
4

對於我的代碼,我使用維基百科API,該維基百科API提供鏈接到與該城市的維基百科文章鏈接的所有地方的鏈接。但是用我的代碼有一些額外的不必要的鏈接。我只想返回類型爲「地標」的鏈接。如何通過API從維基百科獲得地標地點的標題?

我的維基百科API是:

https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type|name|dim|country|region|globe&format=json 

從維基百科API示例JSON數據:

"query": { 
    "geosearch": [ 
     { 
      "pageid": 5858187, 
      "ns": 0, 
      "title": "Stuttgart Hauptbahnhof", 
      "lat": 48.783888888889, 
      "lon": 9.1816666666667, 
      "dist": 136.8, 
      "primary": "", 
      "type": "railwaystation", 
      "name": "", 
      "dim": 1000, 
      "country": "DE", 
      "region": "BW" 
     }, 
     { 
      "pageid": 6102287, 
      "ns": 0, 
      "title": "Staatstheater Stuttgart", 
      "lat": 48.780277777778, 
      "lon": 9.185, 
      "dist": 361, 
      "primary": "", 
      "type": "landmark", 
      "name": "", 
      "dim": "900", 
      "country": "DE", 
      "region": "BW" 
     }, 
     { 
      "pageid": 35806545, 
      "ns": 0, 
      "title": "Versatel building", 
      "lat": 48.78409, 
      "lon": 9.17799, 
      "dist": 400.4, 
      "primary": "", 
      "type": null, 
      "name": "", 
      "dim": 1000, 
      "country": null, 
      "region": null 
     }, 
     { 
      "pageid": 3230957, 
      "ns": 0, 
      "title": "Neue Staatsgalerie", 
      "lat": 48.780277777778, 
      "lon": 9.1869444444444, 
      "dist": 430.6, 
      "primary": "", 
      "type": "landmark", 
      "name": "", 
      "dim": 1000, 
      "country": "DE", 
      "region": "BW" 
     }, 
     .... 
    ] 
} 

我的代碼從這個API得到Title

using (var client = new HttpClient()) 
{ 
    var response = client.GetAsync("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type|name|dim|country|region|globe&format=json").Result; 
    if (response.IsSuccessStatusCode) 
    { 
     var responseContent = response.Content; 
     string responseString = responseContent.ReadAsStringAsync().Result; 
     var obj = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch.Select(a => a.title).ToList(); 

     foreach (var item in obj) 
     { 
      Console.WriteLine(item); 
     } 
    } 
} 

下面是電流輸出:

Output

我怎樣才能得到的結果,其中type是 「里程碑式」 的稱號?

+0

也許,使用'Where'? var obj = JsonConvert.DeserializeObject (responseString).query.geosearch.Where(p => p.type ==「landmark」)。Select(a => a.title).ToList();'? –

回答

3

如果使用JSON格式,試試這個:

var obj = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch 
    .Where(a => a.type == "landmark").Select(a => a.title).ToList(); 

你也可以得到所有標題不使用Json.NET。這是我如何使用XML格式:

using (var webResponse = (HttpWebResponse)WebRequest.Create("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type&format=xml").GetResponse()) 
{ 
    using (var reader = new StreamReader(webResponse.GetResponseStream())) 
    { 
     var response = XElement.Parse(reader.ReadToEnd()); 
     var obj = response.Descendants("gs") 
      .Where(a => a.Attribute("type") != null && a.Attribute("type").Value == "landmark") 
      .Select(a => a.Attribute("title").Value).ToList(); 
    } 
} 
0

我不知道C#:-)

但嘗試這樣的事:

var geosearch = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch; 
var landmarks = geosearch.Where(type => type == "landmark"); 
+0

沒有顯示輸出。 –

+0

那麼,「Where」方法只是進行過濾,您需要使用結果輸出 – neuhaus