Deserializing complex object using Json.NET

前端 未结 3 1399
甜味超标
甜味超标 2021-01-02 04:19

I need to deserialize the this json returned from grogle maps api:

{
    \"destination_addresses\": [
        \"Via Medaglie D\'Oro, 10, 47121 Forlì FC, Ital         


        
3条回答
  •  长情又很酷
    2021-01-02 04:34

    You should deserialize to classes that match your data. You can generate these classes at http://json2csharp.com/.

    // use like
    var rootObj = JsonConvert.DeserializeObject(jsonResponse);
    foreach (var row in rootObj.rows)
    {
        foreach (var element in row.elements)
        {
            Console.WriteLine(element.distance.text);
        }
    }
    
    // you might want to change the property names to .Net conventions
    // use [JsonProperty] to let the serializer know the JSON names where needed
    public class Distance
    {
        public string text { get; set; }
        public int value { get; set; }
    }
    
    public class Duration
    {
        public string text { get; set; }
        public int value { get; set; }
    }
    
    public class Element
    {
        public Distance distance { get; set; }
        public Duration duration { get; set; }
        public string status { get; set; }
    }
    
    public class Row
    {
        public List elements { get; set; }
    }
    
    public class RootObject
    {
        public List destination_addresses { get; set; }
        public List origin_addresses { get; set; }
        public List rows { get; set; }
        public string status { get; set; }
    }
    

提交回复
热议问题