2012-06-29 54 views
1

我从这个google api服务获得json响应,从lat和long获取反向地理位置。用newtonsoft解析JSON

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true

在响应JSON有很多齿条[]相同的名称的。我如何用newtonsoft解析这个JSON来获取国家名称。

+0

作为linkerro说:什么?你尝试过自己(http://www.whathaveyoutried.com) –

+1

@linkerro:如果我没有得到答案,那么我怎么接受的答案,并提高我的速度?像你这样的人只能喊,但不能回答。 –

+2

如果你没有得到答案,可能值得看看提高你的问题质量。这可能有所帮助:http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – ctacke

回答

15
WebClient wc = new WebClient(); 
var json = (JObject)JsonConvert.DeserializeObject(wc.DownloadString(url)); 

var country = json["results"] 
       .SelectMany(x => x["address_components"]) 
       .FirstOrDefault(t => t["types"].First().ToString() == "country"); 

var name = country!=null ? country["long_name"].ToString() : ""; 
+0

非常感谢您的帮助。 –