【发布时间】:2018-11-16 08:52:43
【问题描述】:
所以我想在这个看起来像 Json 文件的 URL 中获取纬度和经度(lat 和 lgn),但是每当我尝试使用 JsonConvert.DeserializeObject 时,我都会收到一个错误...
这是我的代码:
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}");
}
这是我的课:
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; }
}
}
Json 文件 URL 如下:
{
"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"
}
我收到错误消息:“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:''GeocodingApi.Geocoding' 没有'results''的定义”。我尝试了从删除<Geocoding> 到保留它的所有方法......
【问题讨论】: