Search code examples
c#jsonfirebasefirebase-realtime-databasejson-deserialization

c# Deserialize JSON to 3 classes


I have spent 2 days on this problem and still have not solved this...

The Json comes from Firebase Realtime Database.

My absolute goal would be to get the key (Zaptec) as name in the class instead of having to create a "name" property. But that is a question for another day I quess.

My Output:
Name = My Watchlist
Stocks = Models.StocksModel (but value is null)

{
  "name": "My Watchlist",
  "stocks": {
    "Zaptec": {
      "avgPrice": "2,14",
      "name": "Zaptec",
      "shares": 121
    }
  }
}
public class WatchlistModel : INotifyPropertyChanged
{

    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }

    private StocksModel _stocks;
    public StocksModel Stocks
    {
        get { return _stocks; }
        set { _stocks = value; OnPropertyChanged("Stocks"); }
    }
}

public class StocksModel : INotifyPropertyChanged
{
    private StockModel _stocks;
    public StockModel Stocks
    {
        get { return _stocks; }
        set { _stocks = value; OnPropertyChanged("Stocks"); }
    }
}

public class StockModel : INotifyPropertyChanged
{
    #region -- Properties --
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }

    private string _avgPrice;
    public string AvgPrice
    {
        get { return _avgPrice; }
        set { _avgPrice = value; OnPropertyChanged("AvgPrice"); }
    }

    private string _shares;
    public string Shares
    {
        get { return _shares; }
        set { _shares = value; OnPropertyChanged("Shares"); }
    }
}

Solution

  • Your model should look like this:

    public class WatchlistModel : INotifyPropertyChanged
    {
    
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged("Name"); }
        }
    
        private Dictionary<string, StockModel> _stocks;
        public Dictionary<string, StockModel> Stocks
        {
            get { return _stocks; }
            set { _stocks = value; OnPropertyChanged("Stocks"); }
        }
    }
    

    You do not need the StocksModel... You need a dictionary of StockModel. As for how this would work with INotifyPropertyChanged that is a different story. But as for the json the correct model is what is shown on this answer.

    You should be able to do something like JsonConvert.Deserialize(jsonString)

    Also just as a recommendation before you fire the OnPropertyChanged event, you should check if the value actually changed. Something like:

    if(_name != value)
    {
      _name = value;
      OnPropertyChanged("Name");
    }
    

    And lastly your implementation of OnPropertyChanged should use a "Caller" attribute that way you don't have to hardcode the property name like "Name".