我在使用地址时尝试从Google Geofencing API获取经度和纬度。我希望以JSON形式获得结果并使用其中的纬度和经度。当我转到我试图获取信息的链接时,我通过使用JSON格式的特殊粘贴创建了类。
我的问题是,当我去访问类以获取纬度和经度时,它会抛出一个异常,表示"对象引用未设置为对象的实例"和#34; Speech_Recognition.GoogleGeoCodeResponse.geometry.get返回null"。
以下是与之关联的代码:
var url = "https://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";
var result = new System.Net.WebClient().DownloadString(url);
GoogleGeoCodeResponse googleResponse = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(result);
request = new ForecastIORequest("***Removed API KEY***", float.Parse(googleResponse.geometry.location.lat), float.Parse(googleResponse.geometry.location.lng), DateTime.Now, Unit.us); // Error is thrown here
var response = request.Get();
以下是处理JSON repsonse的数据结构:
public class GoogleGeoCodeResponse
{
public Result[] results { get; set; }
public string status { get; set; }
}
public class Result
{
public Address_Components[] address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string place_id { get; set; }
public string[] types { get; set; }
}
public class Geometry
{
public Bounds bounds { get; set; }
public Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
}
public class Bounds
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Northeast
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Southwest
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Location
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Viewport
{
public Northeast1 northeast { get; set; }
public Southwest1 southwest { get; set; }
}
public class Northeast1
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Southwest1
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Address_Components
{
public string long_name { get; set; }
public string short_name { get; set; }
public string[] types { get; set; }
} public string lng { get; set; }
}
以下是结果的内容:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Google Building 41",
"short_name" : "Google Bldg 41",
"types" : [ "premise" ]
},
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Google Bldg 41, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.4228642,
"lng" : -122.0851557
},
"southwest" : {
"lat" : 37.4221145,
"lng" : -122.0859841
}
},
"location" : {
"lat" : 37.4224082,
"lng" : -122.0856086
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238383302915,
"lng" : -122.0842209197085
},
"southwest" : {
"lat" : 37.4211403697085,
"lng" : -122.0869188802915
}
}
},
"place_id" : "ChIJxQvW8wK6j4AR3ukttGy3w2s",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
为什么我会抛出这个NULL异常?我该如何解决这个问题?
答案 0 :(得分:0)
var query = (from a in ....
join b in ...
join c in ...
where (a.Field == field1 || field1 == null)
&& (b.Field == field2 || field2 == null)
select new {a, b, c});
属性位于geometry
内。