简体   繁体   中英

List not being filled by Json deserialize? C#

i'm having difficulties deserialising a json file. The json object has the following structure which has been simplified

    {"date":"2015-11-11",
     "retailer_id":"CLD001",
     "orders":[{
      "products": 
[{
"product_id":"53743443003",
"quantity":4,"
unit_price":42.71}],
"value":170.84,
"customer":{"id":58}}]} 

This structure indicated to me that the top class is

[Table]
public class RetailOrders : INotifyPropertyChanged, INotifyPropertyChanging
{
    private List<OrderItems> oi;
    private string retailer_id;
    private DateTime date;

    public List<OrderItems> OrderItems
    {
        get { return oi; }
        set { oi = value; }
    }
    public string Retailer_id
    {
        get { return retailer_id; }
        set { retailer_id = value; }
    }
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }



    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                          new PropertyChangedEventArgs(propertyName));
        }
    }

    private void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangingEventHandler PropertyChanging;
}

As you can see orders take a list of products being ordered with the variable id, quantity and total price

  [Table]
    public class ProductsOrdered: INotifyPropertyChanged, INotifyPropertyChanging
    {
        private string productID;
        private int quantity;
        private double unit_price;

        public string ProductID
        {
            get { return productID; }
            set { productID = value; }
        }
        public int Quantity
        {
            get { return quantity; }
            set { quantity = value; }
        }
        public double UnitPrice
        {
            get { return unit_price; }
            set { unit_price = value; }
        }



        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                              new PropertyChangedEventArgs(propertyName));
            }
        }

        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

finally the orderItems contain a list of the orders followed by a total price and associated customer

  [Table]
    public class OrderItems : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private List<ProductsOrdered> po = new List<ProductsOrdered>();
        private double TotalPrice;
        private int customer_id;

        public List<ProductsOrdered> Productsordered
        {
            get { return po; }
            set { po = value; }
        }
        public double totalprice
        {
            get { return TotalPrice; }
            set { TotalPrice = value; }
        }
        public int customerid
        {
            get { return customer_id; }
            set { customer_id = value; }
        }



        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                              new PropertyChangedEventArgs(propertyName));
            }
        }

        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;
    }

I'm expecting the list to fill out however currently they are null values and can't seem to think of why this is happening

Your property names don't match the JSON you've supplied. Specifically in your JSON there is an array orders but in your C# class the property is OrderItems .

You could annotate the property like this:

[JsonProperty(PropertyName = "orders")]
public List<OrderItems> OrderItems { get; set; }

I haven't worked all the way through your hierarchy, but any other null fields will likely be caused by a similar mismatch.

One other thing to be careful of is private properties and private setters, which I don't thing will be an issue for you here, but is the other top reason for null values when you deserialize JSON.

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