将json反序列化为模型

时间:2019-06-21 22:40:41

标签: c# json

我有一个像这样的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])转换为type   'Project.Models.JsonDeserializeModel',因为类型需要一个   JSON对象(例如{“ name”:“ value”})以正确反序列化。修理   此错误将JSON更改为JSON对象(例如   {“ name”:“ value”})或将反序列化类型更改为数组或   实现集合接口的类型(例如ICollection,IList)   例如可以从JSON数组反序列化的List。   还可以将JsonArrayAttribute添加到类型中以强制将其   从JSON数组反序列化。路径”,第1行,位置1。'

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

2 个答案:

答案 0 :(得分:1)

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

尝试

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

答案 1 :(得分:0)

这只是一个猜测,而且还未经测试,但是您有嵌套的类(看来至少可以怀疑),请尝试一下。

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