Reputation: 4144
I am trying to deserialize some JSON into a list using JSON.NET; however, there is a number in the way:
Here is the JSON:
"payment_info": {
"fb_id": "",
"order_items": {
"0": {
"product_id": "4534",
"type": "product",
"shipping_cost_per_item": "1.00",
"quantity": "3",
"price_each": "10.00",
"price_total": "30.00"
}
},
Here is my class:
public class OrderItem
{
public string product_id { get; set; }
public string type { get; set; }
public string shipping_cost_per_item { get; set; }
public string quantity { get; set; }
public string price_each { get; set; }
public string price_total { get; set; }
}
public class OrderItems
{
public List<OrderItem> Items { get; set; }
}
How do I tell the converter to ignore the 0? There will be a 1,2,3 for each order item.
Upvotes: 0
Views: 44
Reputation: 129687
I see a few issues here.
First, your JSON is not valid; it appears to be only a fragment. Valid, complete JSON would look like the following. (I added enclosing braces, removed the trailing comma and balanced the braces for the payment_info
object.)
{
"payment_info": {
"fb_id": "",
"order_items": {
"0": {
"product_id": "4534",
"type": "product",
"shipping_cost_per_item": "1.00",
"quantity": "3",
"price_each": "10.00",
"price_total": "30.00"
}
}
}
}
Since your fragment is enclosed in an object, you will need a corresponding top-level class to deserialize into. That class needs a payment_info
property to hold what is now your OrderItems
class. (I would recommend renaming that class to PaymentInfo
if possible to avoid confusion.)
Second, the Items
property inside the OrderItems
class does not match the JSON. In the JSON it is named order_items
. Since they don't match, you will get a null value when you deserialize. You either need to rename the property or use a [JsonProperty]
attribute to specify the JSON property name.
Third, the order_items
property in your JSON isn't a list; it is an object. Therefore you will get an error if you try to deserialize it into a list (once you fix the property name). The usual solution to deal with this situation is to use a Dictionary<string, T>
instead of a List<T>
.
Putting it all together, if you make your classes like this:
public class RootObject
{
public PaymentInfo payment_info { get; set; }
}
public class PaymentInfo
{
public Dictionary<string, OrderItem> order_items { get; set; }
}
public class OrderItem
{
public string product_id { get; set; }
public string type { get; set; }
public string shipping_cost_per_item { get; set; }
public string quantity { get; set; }
public string price_each { get; set; }
public string price_total { get; set; }
}
Then you can deserialize like this:
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
foreach (OrderItem item in root.payment_info.order_items.Values)
{
Console.WriteLine("product id: " + item.product_id);
Console.WriteLine("type: " + item.type);
Console.WriteLine("shipping cost per item: " + item.shipping_cost_per_item);
Console.WriteLine("quantity: " + item.quantity);
Console.WriteLine("price per item: " + item.price_each);
Console.WriteLine("total price: " + item.price_total);
}
Fiddle: https://dotnetfiddle.net/e0t8gX
Upvotes: 1