简体   繁体   中英

C# Deserializing List using Newtonsoft.JSON

I am currently trying to deserialize a list of products and always receiving null for the result of my deserialization. I have reviewed the documentation for serializing and deserializing collections with Newtonsoft.JSON and am still struggling to find the reason why this is occurring. In my current attempt, I am creating a wrapper class products which is meant to hold my list of products. See simple JSON example and model classes below:

JSON example

{  
   "products":[  
      {  
         "title":"Some title",
         "id":123,
         "short_description":"some short description",
         "variations":[
            {  
               "id":234,
               "sku":"N123456789",
               "price":"1.50",
               "stock_quantity":2
            }
         ],
         "some_field_we_dont_care_about":987
      },
      {  
         "title":"A different title",
         "id":123,
         "short_description":"Some other short description",
         "variations":[
            {  
               "id":234,
               "sku":"NG1933410388C",
               "price":"2.00",
               "stock_quantity":800
            }
         ],
         "some_field_we_dont_care_about":123
      }
    ]
}

products.cs

  //wrapper class to facilitate json serialization
  public class products
  {
    List<product> productList { get; set; }
  }

product.cs

  public class product
  {
    public string title { get; set; }

    public int id { get; set; }

    public string short_description { get; set; }

    public List<variation> variations { get; set; }
  }

and variation.cs

  public class variation
  {
    public int id { get; set; }

    public int stock_quantity { get; set; }

    public string sku { get; set; }

    public decimal price { get; set; }
  }

and the code which actually attempts to deserialize the JSON:

products products = JsonConvert.DeserializeObject<products>(productResponse.Content);

The reason this is not serializing is because you don't have a "products" property:

public class products
{
    List<product> productList { get; set; }
}

As you can see, you have a "productList" property.

The best way is to change your class structure to actually match the JSON. You dont have to use horrible casing, you could use proper casing and by default it will work with JSON.NET. There is also an attribute thats part of JSON.NET called [JsonProperty()] . This allows you to have whatever name you want for your C# object, but dictate what the json property name will be on serialization/deserialization:

public class Variation
{
    public int Id { get; set; }

    [JsonProperty(PropertyName = "stock_quantity")]
    public int StockQuantity { get; set; }

    public string Sku { get; set; }

    public decimal Price { get; set; }
}

public class Product
{
    public string Title { get; set; }

    public int Id { get; set; }

    [JsonProperty(PropertyName = "short_description")]
    public string ShortDescription { get; set; }

    public List<Variation> Variations { get; set; }
}

public class ProductCollection
{
    List<Product> Products { get; set; }
}

Then you change your deserialization logic slightly:

var productCollection = JsonConvert.DeserializeObject<ProductCollection>(productResponse.Content);

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