Reputation: 23
So I want to get the latitude and longitude (lat and lgn) in this URL which looks just like a Json file but whenever I try to use JsonConvert.DeserializeObject
I get an error...
Here is my code :
using (WebClient webClient = new System.Net.WebClient())
{
Console.WriteLine("Boucle");
WebClient n = new WebClient();
var json = n.DownloadString(url);
string valueOriginal = Convert.ToString(json);
dynamic GPS = JsonConvert.DeserializeObject<Geocoding>(json);
Console.WriteLine($"{GPS.results}");
}
And here is my class :
public class Geocoding
{
public class Rootobject
{
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 Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Address_Components
{
public string long_name { get; set; }
public string short_name { get; set; }
public string[] types { get; set; }
}
}
The Json file URL is like as below:
{
"results" : [
{
"address_components" : [
{
"long_name" : "41",
"short_name" : "41",
"types" : [ "street_number" ]
},
{
"long_name" : "Boulevard Vauban",
"short_name" : "Boulevard Vauban",
"types" : [ "route" ]
},
{
"long_name" : "Lille",
"short_name" : "Lille",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Nord",
"short_name" : "Nord",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Hauts-de-France",
"short_name" : "Hauts-de-France",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
},
{
"long_name" : "59800",
"short_name" : "59800",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "41 Boulevard Vauban, 59800 Lille, France",
"geometry" : {
"location" : {
"lat" : 50.6341809,
"lng" : 3.0487116
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 50.63552988029151,
"lng" : 3.050060580291502
},
"southwest" : {
"lat" : 50.63283191970851,
"lng" : 3.047362619708498
}
}
},
"place_id" : "ChIJfZ8S2njVwkcRmcb0tz_XNNE",
"types" : [ "establishment", "point_of_interest", "university" ]
}
],
"status" : "OK"
}
I get the error : "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : ''GeocodingApi.Geocoding' doesn't have definition for 'results''". I tried everything from removing the <Geocoding>
to keeping it...
Upvotes: 1
Views: 76
Reputation: 118947
You are trying to deserialise to a nested class. You can either remove the outer class and use the Rootobject
class directly or deserialise with the full namespace:
Geocoding.Rootobject GPS = JsonConvert.DeserializeObject<Geocoding.Rootobject>(json);
Note the variable is of the same type and not dynamic
, there's very rarely a good use case to not use the correct types.
Upvotes: 3
Reputation: 883
Remove the RootObject class definition and collapse its members into Geocoding. Your class structure does not match the JSON file. Alternatively you can Deserialize the string as a "RootObject" rather than a "Geocoding"
Upvotes: 1