所以我想在这个URL中得到纬度和经度(lat和lgn),它看起来就像一个Json文件,但每当我尝试使用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'没有'结果''的定义。”我尝试了从删除<Geocoding>
到保留它......
答案 0 :(得分:2)
您正在尝试反序列化为嵌套类。您可以删除外部类并直接使用Rootobject
类或使用完整命名空间反序列化:
Geocoding.Rootobject GPS = JsonConvert.DeserializeObject<Geocoding.Rootobject>(json);
请注意,变量属于同一类型,而不是dynamic
,因此很少使用正确的类型。
答案 1 :(得分:1)
删除RootObject类定义并将其成员折叠为Geocoding。您的类结构与JSON文件不匹配。或者,您可以将字符串反序列化为“RootObject”而不是“Geocoding”