【问题标题】:Json.net Deserialize Array of Array of classesJson.net 反序列化类数组的数组
【发布时间】:2021-08-19 23:55:59
【问题描述】:

我通过以下 URL 接收 JSON 数据:

 {
       "destination_addresses" : [ "example address" ],
       "origin_addresses" : [ "example address" ],
       "rows" : [
          {
             "elements" : [
                {
                   "distance" : {
                      "text" : "220 ft",
                      "value" : 67
                   },
                   "duration" : {
                      "text" : "1 min",
                      "value" : 12
                   },
                   "status" : "OK"
                }
             ]
          }
       ],
       "status" : "OK"
    }

我正在尝试反序列化为以下类:

public class APIDistance
        {
    
        public class Rootobject
        {
            public string[] destination_addresses { get; set; }
            public string[] origin_addresses { get; set; }
            public Row[] rows { get; set; }
            public string status { get; set; }
        }

        public class Row
        {
            public Element[] elements { get; set; }
        }

        public class Element
        {
            public Distance distance { get; set; }
            public Duration duration { get; set; }
            public string status { get; set; }
        }

        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; }
        }

   }

我的问题是我不知道如何在课堂上分配和/或读取数据。我遵循的示例建议您应该使用 JsonConvert.DeserializeObject 然后使用该对象来获取所需的属性,但是,我似乎无法找到任何属性:

被告知使用 newtonsoft,因为它“简单”,但没有找到我尝试使用的 Json 结构的任何示例。

Json 新手,非常感谢任何帮助。提前致谢。

【问题讨论】:

  • 删除public class APIDistance {,这里只是作为嵌套类的父级。而是像这样 JsonConvert.DeserializeObject<Rootobject>(json) 反序列化 Rootobject
  • 代码图片请勿发,以文字形式粘贴
  • 谢谢查理脸!按我的预期工作。以后我会避免使用代码图片。

标签: c# json json.net


【解决方案1】:

删除 public class APIDistance { 这只是作为一个 嵌套类的父级。而是像这样反序列化 Rootobject JsonConvert.DeserializeObject(json) – Charlieface 18 分钟前

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多