i'm consuming data third party vendor via rest api.
snippet of json api returns:
{ "invoice": { "id": "12345", "lines": { "line1": { "amount": 1, "amount_desc": "", "description": "description 1", "tax_rate": "-1", "price": "100", "discount_pct": 0, "linetotal": 100 }, "line2": { "description": "description 2" }, "line3": { "amount": 1, "amount_desc": "", "description": "description 3", "tax_rate": "-1", "price": "300", "discount_pct": 0, "linetotal": 300 }, "line4": { "description": "description 4" } } } }
i don't know how many "lines" api can return, therefore don't create classes every line object.
my "line" class looks like:
public class lines { public line line1 { get; set; } public line line2 { get; set; } public line line3 { get; set; } public line line4 { get; set; } } public class line { public int amount { get; set; } [jsonproperty("amount_desc")] public string amountdesc { get; set; } public string description { get; set; } [jsonproperty("tax_rate")] public string taxrate { get; set; } public string price { get; set; } [jsonproperty("discount_pct")] public int discountpct { get; set; } public int linetotal { get; set; } }
how can deserialize line1, line2, line100? single "line" class?
you deserialize dictionary<string, line>
:
public class invoice { public string id { get; set; } public dictionary<string, line> lines { get; set; } } public class data { public invoice invoice { get; set; } }
then, can deserialize json in question data
object contains invoice
property. invoice
property of invoice
type contains lines in lines
dictionary.
the line1
line4
strings become keys in dictionary, , values deserialized line
objects.
Comments
Post a Comment