C#,从json模型列表中的列表中获取值(?)

时间:2018-09-01 19:39:54

标签: c# asp.net json

我有一个使用Google Geocoding API使用json2csharp.com制作的json类,但问题是RootObject中有一个列表,因此我无法进一步深入模型并获取所需的值( lat和long)。

public class Model2
{
    public class AddressComponent
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public List<string> types { get; set; }
    }

    public class Location
    {
        public double lat { get; set; }
        public double lng { 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 Viewport
    {
        public Northeast northeast { get; set; }
        public Southwest southwest { get; set; }
    }

    public class Geometry
    {
        public Location location { get; set; }
        public string location_type { get; set; }
        public Viewport viewport { get; set; }
    }

    public class PlusCode
    {
        public string compound_code { get; set; }
        public string global_code { 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 PlusCode plus_code { get; set; }
        public List<string> types { get; set; }
    }

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

这是我下载json字符串并将其反序列化到变量中的代码。最后两行在“ model.results”之后给我错误。

var json = new WebClient().DownloadString("https://maps.googleapis.com/maps/api/geocode/json?address=" + txt1.Text + "&key=APIKEY");
var model = JsonConvert.DeserializeObject<Model2.RootObject>(json);
string lat = model.results.geometry.location.lat.ToString();
string lng = model.results.geometry.location.lng.ToString();

那么我是否缺少有关如何获取所说数据的信息?我是让数据使用Linq之类的唯一选择吗?

0 个答案:

没有答案