Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有嵌套类c的复杂反序列化json#_C#_Json_Nested_Deserialization - Fatal编程技术网

C# 带有嵌套类c的复杂反序列化json#

C# 带有嵌套类c的复杂反序列化json#,c#,json,nested,deserialization,C#,Json,Nested,Deserialization,我已经使用json2csharp生成了类。 var content=response.content都是好的 我还反序列化了主类: var responseData1 = JsonConvert.DeserializeObject<RootObjectChangeLocation>(content); 我试图使var responseData1=JsonConvert.DeserializeObject(content)以检索坐标。但是如果我直接执行,则responseData1中

我已经使用json2csharp生成了类。
var content=response.content都是好的
我还反序列化了主类:

var responseData1 = JsonConvert.DeserializeObject<RootObjectChangeLocation>(content);
我试图使
var responseData1=JsonConvert.DeserializeObject(content)以检索坐标。但是如果我直接执行,则
responseData1
中的
lat
lng
为空。我怎样才能得到它们? 这是json2csharp生成的代码:

 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> types { get; set; }
    }

    public class RootObjectChangeLocation
    {
        public List<Result> results { get; set; }
        public string status { get; set; }
    }

@dbc帮助我根据这一点找到了解决方案

get_坐标。单击+=委托
{
字符串city\u val=change\u location.Text;
var client=新的RestClient(“https://maps.googleapis.com/maps/api/geocode/json?address=");
var请求=新的重新请求(city_val,Method.GET);
IRestResponse response=client.Execute(请求);
var content=response.content;
var responseData1=JsonConvert.DeserializeObject(内容);
如果(内容==null | |内容==“”)
{
Toast.MakeText(此“城市为空或不正确”,ToastLength.Short).Show();
}
其他的
{
foreach(responseData1.results中的var数据)
{
var lat=data.geometry.location.lat;
}
}
//Toast.MakeText(this,content,ToastLength.Long).Show();
Console.WriteLine(content.ToString());
};

通过property responseData1.Geometry.location访问位置时,您会收到什么?
 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> types { get; set; }
    }

    public class RootObjectChangeLocation
    {
        public List<Result> results { get; set; }
        public string status { get; set; }
    }
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Vinnytsia",
               "short_name" : "Vinnytsia",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vinnyts'ka city council",
               "short_name" : "Vinnyts'ka city council",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Vinnyts'ka oblast",
               "short_name" : "Vinnyts'ka oblast",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Ukraine",
               "short_name" : "UA",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vinnytsia, Vinnyts'ka oblast, Ukraine",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 49.27902,
                  "lng" : 28.5710879
               },
               "southwest" : {
                  "lat" : 49.190448,
                  "lng" : 28.3681799
               }
            },
            "location" : {
               "lat" : 49.233083,
               "lng" : 28.468217
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 49.27902,
                  "lng" : 28.5710879
               },
               "southwest" : {
                  "lat" : 49.1906116,
                  "lng" : 28.3681799
               }
            }
         },
         "place_id" : "ChIJiWRaGWVbLUcR_nTd7lnh1Ms",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}
        get_coordinates.Click += delegate
        {
            string city_val = change_location.Text;
            var client = new RestClient("https://maps.googleapis.com/maps/api/geocode/json?address=");
            var request = new RestRequest(city_val, Method.GET);
            IRestResponse response = client.Execute(request);
            var content = response.Content;

            var responseData1 = JsonConvert.DeserializeObject<RootObjectChangeLocation>(content);

            if (content == null||content=="")
            {
                Toast.MakeText(this, "City is empty or incorrect", ToastLength.Short).Show();
            }
            else
            {
                foreach(var data in responseData1.results)
                {
                    var lat = data.geometry.location.lat;
                }
            }
            // Toast.MakeText(this, content, ToastLength.Long).Show();
            Console.WriteLine(content.ToString());
        };