C# 对象引用未设置为对象的实例(Google Geo Fenging API)
在使用地址时,我试图从谷歌地理围栏API获取纬度和经度。我希望以JSON格式获得结果,并使用其中的纬度和经度。当我转到我试图从中获取信息的链接时,我通过粘贴JSON格式的special创建了类 我的问题是,当我访问类以获取纬度和经度时,它会抛出一个异常,即“对象引用未设置为对象的实例”和“Speech_Recognition.GoogleGeoCodeResponse.geometry.get returned null” 以下是与其关联的代码:C# 对象引用未设置为对象的实例(Google Geo Fenging API),c#,json,google-geocoding-api,C#,Json,Google Geocoding Api,在使用地址时,我试图从谷歌地理围栏API获取纬度和经度。我希望以JSON格式获得结果,并使用其中的纬度和经度。当我转到我试图从中获取信息的链接时,我通过粘贴JSON格式的special创建了类 我的问题是,当我访问类以获取纬度和经度时,它会抛出一个异常,即“对象引用未设置为对象的实例”和“Speech_Recognition.GoogleGeoCodeResponse.geometry.get returned null” 以下是与其关联的代码: var url = "https://maps.
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();
以下是结果的内容:
{
"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"
}
为什么要抛出这个空异常?如何修复此问题?几何体的
属性位于结果的内部,因此我将其更改为:结果googleResponse=JsonConvert.DeserializeObject(结果);请求=新的预测请求(“ea3015a917dba9f4f679aa189febff24”,googleResponse.geometry.location.lat,googleResponse.geometry.location.lng,DateTime.Now,Unit.us)代码>并得到相同的错误。
{
"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"
}