use*_*704 2 c# json google-maps
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);
Run Code Online (Sandbox Code Playgroud)
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?)
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; }
}
Run Code Online (Sandbox Code Playgroud)
And you have to do
RootObject rootObject = new JavaScriptSerializer().Deserialize<RootObject>(content);
Run Code Online (Sandbox Code Playgroud)
Try referring to this stackoverflow posting if you would prefer other ways Deserialize JSON with C#
| 归档时间: |
|
| 查看次数: |
1162 次 |
| 最近记录: |