简体   繁体   中英

Deserializing JSON produces default class

I am deserializing an Object using this code

PlayerData = JsonConvert.DeserializeObject<UserData>(response.Content);

My UserData class is as follows

    public class UserData : BindableBase
    {
        public string UID { get; set; }
        public string UDID { get; set; }
        public object liveID { get; set; }
        public int timeLastSeen { get; set; }
        public string country { get; set; }
        public string username { get; set; }
        public string session { get; set; }
        public int serverTime { get; set; }
        public int PremiumCurrency { get; set; }

        public UserData()
        {

        }
    }

When it deserializes I get an object returned as if I had just called new UserData() but if I remove the : BindableBase from the class definition it deserializes correctly. I guess this is because the base class contains some properties that arent in the JSON but I just want those ignored. Is there a setting for this?

The BindableBase class is as follows

[Windows.Foundation.Metadata.WebHostHidden]
[DataContract]
public abstract class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
    {
        if (object.Equals(storage, value)) return false;

        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The JSON is as follows

{
    "UID": "4",
    "UDID": "",
    "liveID": null,
    "timeLastSeen": 1392730436,
    "country": "GB",
    "username": "User4",
    "PremiumCurrency": "20",
    "session": "5c8583311732fa816e333dc5e6426d65",
    "serverTime": 1392730437
}

It's clear enough to answer that the problem is in DataContract attribute of base class. Easy way to fix it is to decorate properties of derived class with DataMember attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM