【问题标题】:Get data from a string/Json url从字符串/Json url 获取数据
【发布时间】: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''的定义”。我尝试了从删除&lt;Geocoding&gt; 到保留它的所有方法......

【问题讨论】:

    标签: c# json geocoding


    【解决方案1】:

    您正在尝试反序列化为嵌套类。您可以删除外部类并直接使用 Rootobject 类或使用完整命名空间反序列化:

    Geocoding.Rootobject GPS = JsonConvert.DeserializeObject<Geocoding.Rootobject>(json);
    

    请注意,变量的类型相同,而不是dynamic,很少有不使用正确类型的好用例。

    【讨论】:

    • 所以,我尝试更改它,现在(感谢您)我没有收到错误消息,但当我尝试 Console.WriteLine($"{GPS.results}") 时,我只是在控制台上收到 GeocodingApi.Geocoding+Result[]
    • 那是因为$"{GPS.results}" 字符串插值正在打印出你的类型。如果您想知道每个单独的值,则需要遍历该数组。
    • 非常感谢!当它读到我没有正确思考时,我感到非常失望!
    【解决方案2】:

    删除 RootObject 类定义并将其成员折叠到地理编码中。您的类结构与 JSON 文件不匹配。或者,您可以将字符串反序列化为“RootObject”而不是“地理编码”

    【讨论】:

      猜你喜欢
      • 2016-03-19
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多