使用newtonsoft解析JSON

Ars*_*aiz 1 c# json json.net windows-phone-7

我从这个google api服务获得json响应,从lat和long获得反向地理位置.

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

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

L.B*_*L.B 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() : "";
Run Code Online (Sandbox Code Playgroud)