从 string/Json url 获取数据
Get data from a string/Json url
所以我想在这个 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}");
}
这是我的 class:
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' doesn't have definition for 'results''"。我尝试了从删除 <Geocoding>
到保留它的所有方法...
删除 RootObject class 定义并将其成员折叠到地理编码中。您的 class 结构与 JSON 文件不匹配。或者,您可以将字符串反序列化为 "RootObject" 而不是 "Geocoding"
您正在尝试反序列化为嵌套 class。您可以删除外部 class 并直接使用 Rootobject
class 或使用完整命名空间进行反序列化:
Geocoding.Rootobject GPS = JsonConvert.DeserializeObject<Geocoding.Rootobject>(json);
注意变量是同一类型而不是 dynamic
,很少有不使用正确类型的好用例。
所以我想在这个 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}");
}
这是我的 class:
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' doesn't have definition for 'results''"。我尝试了从删除 <Geocoding>
到保留它的所有方法...
删除 RootObject class 定义并将其成员折叠到地理编码中。您的 class 结构与 JSON 文件不匹配。或者,您可以将字符串反序列化为 "RootObject" 而不是 "Geocoding"
您正在尝试反序列化为嵌套 class。您可以删除外部 class 并直接使用 Rootobject
class 或使用完整命名空间进行反序列化:
Geocoding.Rootobject GPS = JsonConvert.DeserializeObject<Geocoding.Rootobject>(json);
注意变量是同一类型而不是 dynamic
,很少有不使用正确类型的好用例。