I am using a thirdparty API and the Json response that I receive back is as below. Usually, using something like
Newtonsoft.Json.JsonConvert.DeserializeObject<MyModel>(response);
I can easily create a .Net model. However, I am struggling on how to formulate the below. I've tried KeyValuePairs, strings, modifying the actual Json but cannot seem to get any joy.
What am I missing?
{
"1": [
{
"qty": 1,
"discount": "flat",
"price": 71.68
}
],
"2": [
{
"qty": 1,
"discount": "flat",
"price": 62.75
}
],
"3": [
{
"qty": 1,
"discount": "flat",
"price": 77.28
}
],
"4": [
{
"qty": 1,
"discount": "flat",
"price": 82.88
}
],
"5": [
{
"qty": 1,
"discount": "flat",
"price": 67.84
}
]
}
Now, what is throwing me is that the numbers(1,2,3,4,5) are Identifiers so will not stay constant and could change each time you receive the response.
Dictionary<string, MyClass>? (where MyClass is qty, discount, price) (or maybe Dictionary<string, MyClass[]> because although there is only 1 object per index, it appears to be an array).JObjectand/or a custom serialiser for that JSON. I'd suggest your root cause here is whatever is generating that JSON. I'm presuming it's supposed to be an array but it looks like it's parsing the array index as an attribute, fix that not the parsing hereMyModellook like? It seems, thatDictionary<string, MyModel[]>(value should be an array) will help you