我有来自Google地理编码API的JSON blob,其中包含城市名称。我只是在寻找linq表达式来解决它。
JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "277 Bedford Avenue, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.714232,
"lng" : -73.9612889
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.7155809802915,
"lng" : -73.9599399197085
},
"southwest" : {
"lat" : 40.7128830197085,
"lng" : -73.96263788029151
}
}
},
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Grand St/Bedford Av",
"short_name" : "Grand St/Bedford Av",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Grand St/Bedford Av, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.714321,
"lng" : -73.961151
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.71566998029149,
"lng" : -73.95980201970849
},
"southwest" : {
"lat" : 40.7129720197085,
"lng" : -73.96249998029151
}
}
},
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "Grand St/Bedford Av",
"short_name" : "Grand St/Bedford Av",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11249",
"short_name" : "11249",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Grand St/Bedford Av, Brooklyn, NY 11249, USA",
"geometry" : {
"location" : {
"lat" : 40.714684,
"lng" : -73.961563
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.71603298029149,
"lng" : -73.9602140197085
},
"southwest" : {
"lat" : 40.7133350197085,
"lng" : -73.96291198029151
}
}
},
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "Bedford Av/Grand St",
"short_name" : "Bedford Av/Grand St",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Bedford Av/Grand St, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.71471,
"lng" : -73.960999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.7160589802915,
"lng" : -73.95965001970849
},
"southwest" : {
"lat" : 40.7133610197085,
"lng" : -73.9623479802915
}
}
},
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "New York",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Brooklyn, NY 11211, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.7280089,
"lng" : -73.9207299
},
"southwest" : {
"lat" : 40.7008331,
"lng" : -73.9644697
}
},
"location" : {
"lat" : 40.7093358,
"lng" : -73.9565551
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.7280089,
"lng" : -73.9207299
},
"southwest" : {
"lat" : 40.7008331,
"lng" : -73.9644697
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "New York",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Williamsburg, Brooklyn, NY, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.7251773,
"lng" : -73.936498
},
"southwest" : {
"lat" : 40.6979329,
"lng" : -73.96984499999999
}
},
"location" : {
"lat" : 40.7064461,
"lng" : -73.95361629999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.7251773,
"lng" : -73.936498
},
"southwest" : {
"lat" : 40.6979329,
"lng" : -73.96984499999999
}
}
},
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Kings County, NY, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.739446,
"lng" : -73.83336509999999
},
"southwest" : {
"lat" : 40.5695637,
"lng" : -74.0429503
}
},
"location" : {
"lat" : 40.6528762,
"lng" : -73.95949399999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.739446,
"lng" : -73.83336509999999
},
"southwest" : {
"lat" : 40.5695637,
"lng" : -74.0429503
}
}
},
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "New York",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Brooklyn, NY, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.739446,
"lng" : -73.83336509999999
},
"southwest" : {
"lat" : 40.551042,
"lng" : -74.05663
}
},
"location" : {
"lat" : 40.6781784,
"lng" : -73.94415789999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.739446,
"lng" : -73.83336509999999
},
"southwest" : {
"lat" : 40.551042,
"lng" : -74.05663
}
}
},
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "New York",
"short_name" : "New York",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New York County",
"short_name" : "New York County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New York, NY, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.91525559999999,
"lng" : -73.70027209999999
},
"southwest" : {
"lat" : 40.4959961,
"lng" : -74.2590879
}
},
"location" : {
"lat" : 40.7127837,
"lng" : -74.0059413
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.91525559999999,
"lng" : -73.70027209999999
},
"southwest" : {
"lat" : 40.496006,
"lng" : -74.25573489999999
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New York, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 45.015865,
"lng" : -71.85626429999999
},
"southwest" : {
"lat" : 40.4959961,
"lng" : -79.76214379999999
}
},
"location" : {
"lat" : 43.2994285,
"lng" : -74.21793260000001
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 45.015865,
"lng" : -71.85626429999999
},
"southwest" : {
"lat" : 40.4960191,
"lng" : -79.76214379999999
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "United States",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 71.389888,
"lng" : -66.94976079999999
},
"southwest" : {
"lat" : 18.9110642,
"lng" : 172.4458955
}
},
"location" : {
"lat" : 37.09024,
"lng" : -95.712891
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 49.38,
"lng" : -66.94
},
"southwest" : {
"lat" : 25.82,
"lng" : -124.39
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
这是我的标准: 给定“结果”中的“结果”,结果“类型”包含“地点” 为了得到结果,我希望'address_components'中的项目'types'包含'locality'
更新:
这很有效,但似乎可以更简洁:
if (responseObject["results"].Children().Count() > 0)
{
var postalResult = responseObject["results"].Children().FirstOrDefault((result) =>
{
return result["types"].Children().Contains("locality");
});
if(postalResult !=null)
{
var addressComponent = postalResult["address_components"].FirstOrDefault((ac) =>
{
return ac["types"].Children().Contains("locality");
});
if(addressComponent != null)
{
cityFromGoogle = addressComponent["long_name"].Value<string>().ToLower();
}
}
}
答案 0 :(得分:0)
如果你将你的json转换为具体的类,你可以轻松地处理它们
var resp = JsonConvert.DeserializeObject<Google.Response>(json);
var addresses = resp.results.Where(x => x.address_components.Any(a => a.types.Contains("locality")))
.ToList();
public class Google
{
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Bounds
{
public Location northeast { get; set; }
public Location southwest { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public string location_type { get; set; }
public Bounds viewport { get; set; }
public Bounds bounds { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public List<string> types { get; set; }
}
public class Response
{
public List<Result> results { get; set; }
public string status { get; set; }
}
}