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

假装没事ソ 提交于 2019-12-07 00:30:16

问题


I got stuck with Json.NET library and its DeserializeObject method. The Documentation is not quite clear with what could be happening here, so I would appreciate if somebody could explain how to acheive to deserialize JSON into list of User objects.

I'm 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
    }
]

into an 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. Not getting any error, nor users. JsonConvert.DeserializeObject just dies silently.

I tried to create mockup and to do SerializeObject and DeserializeObject with that JSON string but with same result, no errors, no results.

I even try to pass serializersettings in order to se what is wrong but with no errors either

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 is happening during deserialization I notice that context is not initialized?!

public class User
{
    ...
    [OnDeserializing]
    internal void OnDeserializing(StreamingContext context) {
    }
}

What I'm doing wrong here? And how I can deserialize this into an list of Users?


回答1:


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?




回答2:


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




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/2813350/json-net-deserializeobjectlistt-not-returning-list-nor-giving-error

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