【问题标题】:Deserialize json into model [duplicate]将json反序列化为模型[重复]
【发布时间】:2019-06-21 22:40:41
【问题描述】:

我有一个这样的 json:

[  
   {  
      \"childNodes\":null,
      \"children\":null,
      \"key\":\"\",
      \"subKey\":{  
         \"buffer\":\"\",
         \"offset\":0,
         \"length\":0,
         \"value\":\"\",
         \"hasValue\":true
      },
      \"isContainerNode\":false,
      \"rawValue\":null,
      \"attemptedValue\":null,
      \"errors\":[  
         {  
            \"exception\":null,
            \"errorMessage\":\"Incorrect password.\"
         }
      ],
      \"validationState\":1
   }
]

所以我想把它反序列化成模型,所以我创建了这样的模型:

 public  class JsonDeserializeModel
    {

        public class SubKey
        {
            public string buffer { get; set; }
            public int offset { get; set; }
            public int length { get; set; }
            public string value { get; set; }
            public bool hasValue { get; set; }
        }

        public class Error
        {
            public object exception { get; set; }
            public string errorMessage { get; set; }
        }

        public class RootObject
        {
            public object childNodes { get; set; }
            public object children { get; set; }
            public string key { get; set; }
            public SubKey subKey { get; set; }
            public bool isContainerNode { get; set; }
            public object rawValue { get; set; }
            public object attemptedValue { get; set; }
            public List<Error> errors { get; set; }
            public int validationState { get; set; }
        }
    }

然后我尝试反序列化为:

  JsonDeserializeModel completeObject = JsonConvert.DeserializeObject<JsonDeserializeModel>(response.content);

但它会抛出异常:

Newtonsoft.Json.JsonSerializationException: '无法反序列化 当前 JSON 数组(例如 [1,2,3])转换为类型 'Project.Models.JsonDeserializeModel' 因为该类型需要 JSON 对象(例如 {"name":"value"})正确反序列化。修理 此错误要么将 JSON 更改为 JSON 对象(例如 {"name":"value"}) 或将反序列化类型更改为数组或 实现集合接口的类型(例如 ICollection、IList) 比如可以从 JSON 数组反序列化的 List。 JsonArrayAttribute 也可以添加到类型中以强制它 从 JSON 数组反序列化。路径'',第 1 行,位置 1。'

我不明白错误在哪里。有人可以知道或有更多经验知道什么是错的吗?问候

【问题讨论】:

  • JsonConvert.DeserializeObject&lt;List&lt;RootObject&gt;&gt;

标签: c# json


【解决方案1】:

您似乎正在尝试将 json 数组反序列化为对象。 此外,您需要引用嵌套对象类型 RootObject。

试试

JsonDeserializeModel completeObject = JsonConvert.DeserializeObject<JsonDeserializeModel.RootObject[]>(response.content);

【讨论】:

    【解决方案2】:

    这只是一个猜测并且完全未经测试,但是您有嵌套类(至少可以说这似乎是可疑的)试试这个。

    var completeObject = JsonConvert.DeserializeObject<JsonDeserializeModel.RootObject>(response.content);
    

    或者更好,删除类嵌套

    public class SubKey
    {
        public string buffer { get; set; }
        public int offset { get; set; }
        public int length { get; set; }
        public string value { get; set; }
        public bool hasValue { get; set; }
    }
    
    public class Error
    {
        public object exception { get; set; }
        public string errorMessage { get; set; }
    }
    
    public class RootObject
    {
        public object childNodes { get; set; }
        public object children { get; set; }
        public string key { get; set; }
        public SubKey subKey { get; set; }
        public bool isContainerNode { get; set; }
        public object rawValue { get; set; }
        public object attemptedValue { get; set; }
        public List<Error> errors { get; set; }
        public int validationState { get; set; }
    }
    
    ...
    
    var completeObject = JsonConvert.DeserializeObject<RootObject>(response.content);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-05
      • 2017-04-13
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      相关资源
      最近更新 更多