Json反序列化未填写列表? C#

时间:2019-01-21 11:36:27

标签: c# json list

我在反序列化json文件时遇到困难。 json对象具有以下经过简化的结构

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

这种结构向我表明,顶级是

[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;
}

如您所见,订单中列出了所订购产品的ID,数量和总价的变量

  [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));
            }
        }

最后,orderItems包含订单列表,后跟总价和相关客户

  [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;
    }

我希望列表能够填写,但是目前它们是空值,似乎无法考虑为什么会发生

1 个答案:

答案 0 :(得分:0)

您的属性名称与您提供的JSON不匹配。特别是在JSON中,有一个数组orders,但在C#类中,属性是OrderItems

您可以这样注释属性:

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

我还没有完全了解您的层次结构,但是其他任何空字段都可能是由类似的不匹配引起的。

要特别注意的另一件事是私有属性和私有设置器,我认为这对您来说不是问题,但是当您反序列化JSON时,空值的另一个主要原因就是如此。