2017-06-18 19 views
0

住所を使用するときにGoogle Geofencing APIから緯度と経度を取得しようとしています。結果をJSON形式で取得し、そこから緯度と経度を使用したいと考えています。情報を取得しようとしているリンクに移動するときに、JSON形式のスペシャルを貼り付けることでクラスを作成しました。オブジェクト参照がオブジェクトのインスタンスに設定されていない(Google Geo-fencing API)

私の問題は、クラスにアクセスして緯度と経度を取得すると、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」と「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

'result'の内容を質問に投稿してください。 –

+0

@NickLarsen –

答えて

0

geometryのプロパティは、resultsです。

+0

を追加しましたので、これを変更しました: '結果googleResponse = JsonConvert.DeserializeObject (結果); リクエスト=新しいForecastIORequest( "ea3015a917dba9f4f679aa189febff24"、googleResponse.geometry.location.lat、googleResponse.geometry.location.lng、DateTime.Now、Unit.us);および同じエラーが発生しました。 –

関連する問題