JSon.NET DeserializeObject <List <T>> does not return a list and does not give an error
I am stuck in the Json.NET library and its DeserializeObject method. The documentation is not entirely clear with what can happen here, so I would appreciate it if someone could explain how to get JSON deserialized into a list of User objects.
I am trying to deserialize this JSON
[ {"userid":"0", "listid":1, "lastname":"Mann", "inplace":true, "xpos":428, "ypos":111 }, {"userid":"1", "listid":1, "lastname":"Parker", "inplace":true, "xpos":334, "ypos":154 }, {"userid":"2", "listid":1, "lastname":"Terry", "inplace":true, "xpos":513, "ypos":160 } ] to user object
[JsonObject(MemberSerialization.OptIn)] public class User { [JsonProperty(PropertyName = "userid")] public string userid { get; set; } [JsonProperty(PropertyName = "listid")] public int listid { get; set; } [JsonProperty(PropertyName = "lastname")] public string lastname { get; set; } [JsonProperty(PropertyName = "inplace")] public bool inplace { get; set; } [JsonProperty(PropertyName = "xpos")] public int xpos { get; set; } [JsonProperty(PropertyName = "ypos")] public int ypos { get; set; } public User() { } } using
List<User> users = JsonConvert.DeserializeObject<List<User>>(jsonUsers); without success. No errors are received, nor users. JsonConvert.DeserializeObject just dies quietly.
I tried to create a layout and execute SerializeObject and DeserializeObject using this JSON string, but with the same result, with no errors and no results.
I am even trying to pass serializersettings in order to determine what is wrong, but without errors.
JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Error += delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) { errorList.Add(args.ErrorContext.Error.Message); }; List<User> users = JsonConvert.DeserializeObject<List<User>>(jsonUsers, settings); When I try to see what happens during deserialization, did I notice that the context is not initialized ?!
public class User { ... [OnDeserializing] internal void OnDeserializing(StreamingContext context) { } } What am I doing wrong here? And how can I deserialize this to a user list?
I found that was the problem. JSon.NET deserialized my integers from JSON to Int64 instead of Int32, so I set a lot of time instead of int, and everything worked as it should in the first place.
Is there a way to indicate that I want properties to be deserialized in int?
At this point, you could solve the problems, but if anyone stumbles upon this, this may help.
I am using the Json.net library to deserialize custom complex hierarchical objects. I have a couple of converters for converting from Json to C # objects.
You can use JsonConvertAttributes to apply return property types.
[JsonConverter(typeof(Int32))] public int xpos { get; set; } If you want to return a custom serialization object, you can write your own converters, and Json.net will use / convert the de-serialized object using this custom converter.
Sanjay Zalke
I have the same problem because I do not have a default constructor (constructor with empty parameters) in my class
I also had no luck with List of T, with no errors, but only with zero results:
List<SomeClass> obj = JsonConvert.DeserializeObject<List<SomeClass>>(jsonstring); public class SomeClass { public string SomeProp {get;set;} (...) } For me there was the following solution. I can only guess about the explanation, so I think you are better off with the code:
SomeClassList obj = JsonConvert.DeserializeObject<SomeClassList>(jsonstring); public class SomeClass { public string SomeProp {get;set;} (...) } public class SomeClassList { public List<SomeClass> SomeClass {get;set;} } Hope this helps. (If not, good luck sorting all that damn json "zero setting" when searching -.-)