我正在尝试使用google maps api。这个URL完全符合我的要求:
http://maps.googleapis.com/maps/api/geocode/json?address=77379
看看结果。它有我需要的所有信息... lat,lon,state,country。麻烦的是,我不知道如何提取这些数据。我试过这个:
var client = new WebClient();
var content = client.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?address=77379");
object myObject = JsonConvert.DeserializeObject(content);
虽然这没有错误,但myObject
最终没有任何用处。 (或者,也许它是,我只是不知道它?)
答案 0 :(得分:4)
这是你的班级结构
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; }
}
你必须这样做
RootObject rootObject = new JavaScriptSerializer().Deserialize<RootObject>(content);
如果您希望采用其他方式,请尝试参考此stackoverflow发布 Deserialize JSON with C#
答案 1 :(得分:1)
根据我使用JSON的经验,我一直使用这种方法:
object myObject = JsonConvert.DeserializeObject(content);
那会有用吗?或者这与你正在做的事情有什么不同?