如何通过NewtonSoft反序列化对象json列表?

时间:2021-01-03 09:57:59

I Serialize below class by Newtonsoft.Json but i can't Deserialize the same json by Newtonsoft.Json. How can i do that?

我通过Newtonsoft.Json在类下面进行序列化,但是我无法通过Newtonsoft.Json反序列化相同的json。我怎样才能做到这一点?

Json:

JSON:

"{\"UserEvents\":[{\"id\":1214308,\"Date\":20150801000000,\"IsRead\":true}]}"

My Entities:

我的实体:

   public class UserEventLog {
    [JsonProperty("UserEvents")]
    public List<UserEvent> UserEvents { get; set; }
    public UserEventLog() {
        UserEvents = new List<UserEvent>();
    }
}


public class UserEvent {
    [JsonProperty("id")]
    public long id{ get; set; }
      [JsonProperty("Date")]
    public long Date{ get; set; }
      [JsonProperty("IsRead")]
    public bool IsRead { get; set; }
}

My Deserializer is that :

我的解串器是这样的:

  List<UserEventLog> convert = JsonConvert.DeserializeObject<List<UserEventLog>>(user.ToString()) as List<UserEventLog>;

But Error is produced:

但错误产生:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Newtonsoft.Json.dll中出现未处理的“Newtonsoft.Json.JsonSerializationException”类型异常

Additional information: Error converting value "{"UserEvents":[{"id":1214308,"Date":20150801000000,"IsRead":true}]}" to type 'System.Collections.Generic.List`1

其他信息:转换值“{”UserEvents“时出错:[{”id“:1214308,”日期“:20150801000000,”IsRead“:true}]}”键入'System.Collections.Generic.List`1

How can i solve it? how can i Deserialize my List of object to UserEvents list?

我怎么解决呢?我怎样才能将我的对象列表反序列化为UserEvents列表?

1 个解决方案

#1


4  

This works in linqpad:

这适用于linqpad:

void Main()
{
    var user = "{\"UserEvents\":[{\"id\":1214308,\"Date\":20150801000000,\"IsRead\":true}]}";
    UserEventLog convert = JsonConvert.DeserializeObject<UserEventLog>(user.ToString());
    convert.UserEvents.Count().Dump();
}

public class UserEventLog {
    [JsonProperty("UserEvents")]
    public List<UserEvent> UserEvents { get; set; }
    public UserEventLog() {
        UserEvents = new List<UserEvent>();
    }
}


public class UserEvent {
    [JsonProperty("id")]
    public long id{ get; set; }
      [JsonProperty("Date")]
    public long Date{ get; set; }
      [JsonProperty("IsRead")]
    public bool IsRead { get; set; }
}

The problem is that you're trying to deserialize into list, but it is not an array of UserEvents

问题是您正在尝试反序列化到列表中,但它不是UserEvents数组

#1


4  

This works in linqpad:

这适用于linqpad:

void Main()
{
    var user = "{\"UserEvents\":[{\"id\":1214308,\"Date\":20150801000000,\"IsRead\":true}]}";
    UserEventLog convert = JsonConvert.DeserializeObject<UserEventLog>(user.ToString());
    convert.UserEvents.Count().Dump();
}

public class UserEventLog {
    [JsonProperty("UserEvents")]
    public List<UserEvent> UserEvents { get; set; }
    public UserEventLog() {
        UserEvents = new List<UserEvent>();
    }
}


public class UserEvent {
    [JsonProperty("id")]
    public long id{ get; set; }
      [JsonProperty("Date")]
    public long Date{ get; set; }
      [JsonProperty("IsRead")]
    public bool IsRead { get; set; }
}

The problem is that you're trying to deserialize into list, but it is not an array of UserEvents

问题是您正在尝试反序列化到列表中,但它不是UserEvents数组