Reputation: 13955
I'm trying to work with the google maps api. This URL is EXACTLY what I need:
http://maps.googleapis.com/maps/api/geocode/json?address=77379
Take a look at the results. It has all the info I need... lat, lon, state, country. Trouble is, I don't know how to extract this data. I tried this:
var client = new WebClient();
var content = client.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?address=77379");
object myObject = JsonConvert.DeserializeObject(content);
While that doesn't error out, myObject
doesn't end up being anything useful. (Or, maybe it is and I just don't know it?)
Upvotes: 2
Views: 1523
Reputation: 2239
Here is your class structure
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { 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 Bounds
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast2 northeast { get; set; }
public Southwest2 southwest { 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 Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string place_id { get; set; }
public List<string> postcode_localities { get; set; }
public List<string> types { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public string status { get; set; }
}
And you have to do
RootObject rootObject = new JavaScriptSerializer().Deserialize<RootObject>(content);
Try referring to this stackoverflow posting if you would prefer other ways Deserialize JSON with C#
Upvotes: 5
Reputation:
From my experience using JSON I have always used this method:
object myObject = JsonConvert.DeserializeObject(content);
Would that work? Or is this something different from what you're doing?
Upvotes: 1