JSon.NET DeserializeObject<List<T>> not returning list nor giving error

核能气质少年 提交于 2019-12-05 05:19:14

I found what was problem. JSon.NET was deserializing my integers from JSON to Int64 instead to Int32 so instead of int I put long and everything worked as it should in the first place.

Is there a way to specify that I want that properties to deserialize into int?

By this time you might have resolved the issues, but if any one stumble upon this might help.

I am using Json.net library to de-serialise custom complex hierarchical objects. I have couple of converters to convert from Json to c# objects.

You can use JsonConvertAttributes to cast the return types for properties.

[JsonConverter(typeof(Int32))] 
public int xpos { get; set; }

If you want to return a custom de-serialise object you can write your own converters and Json.net will cast/convert the de-serialised object using this custom converter.

Sanjay Zalke

I have quite the same problem it was because i have no default constructor(constructor with empty parameters) in my class

I had no luck with a List of T either, no error but only null results:

List<SomeClass> obj = JsonConvert.DeserializeObject<List<SomeClass>>(jsonstring);

public class SomeClass  
{
     public string SomeProp {get;set;}
     (...)
}

For me the following was the Solution. I can only guess the explanation, so I think you're better of with just 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 it helps. (If not, good luck sorting out all that darn json "null setting" stuff while searching -.-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!