Good day,
i was hoping someone could possible assist me.
i am trying to Deserialize Json data received from an API call.
the ShopList data looks like this
{ "id": "1", "name": "Ice Cream yay", "description": "Best Ice Cream in Town", "flavors": null }, { "id": "2", "name": "Million Favors Ice cream", "description": "We have it all", "flavors": { "Chocolate": "$0.39", "Lime": "$0.41" } }, { "id": "3", "name": "Hot Ice Cream", "description": "Hottest Ice Cream in Town", "flavors": null },
Historically i was doing this with the following method.
var Shops = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(ShopList); var ShopNames = items.Select(d => d["name"]).ToList();
var Shopdescription = items.Select(d => d["description"]).ToList();
to build a list of all the shop names and descriptions but recently the Flavors container has been added and now i'm receiving the following error.
Newtonsoft.Json.JsonReaderException
Message=Unexpected character encountered while parsing value: {. Path '[37].flavors', line 1, position 1528.
Your model is something like this:
public class Flavors {
public string Chocolate { get; set; }
public string Lime { get; set; }
} public class MyArray { public string id { get; set; } public string name { get; set; } public string description { get; set; } public Flavors flavors { get; set; } } public class Root { public List<MyArray> MyArray { get; set; } }
So you should deserialize it with
List array = JsonConvert.DeserializeObject<List>(json);
I think you have to say which is the type to deserialize
List<Root> I suppose JsonConvert.DeserializeObject<List<Root>>(IceCreamJsonData)>
Answers
Your model is something like this:
public class Flavors {
public string Chocolate { get; set; }
public string Lime { get; set; }
So you should deserialize it with
List array = JsonConvert.DeserializeObject<List>(json);
Hi Alessandro,
Thank you for the assistance (also on a side note thank for all your posts a couple of your answers to other peoples questions have helped me in the past.)
Thank you for explaining how the structure works. I see how you have classified each container with the json response. this will be very helpful going forward. if something later in life was added E.G toppings i'm assuming it would look like this.
`//Json data called IceCreamJsonData:
[
{ "id": "1",
"name": "Ice Cream yay",
"description": "Best Ice Cream in Town",
"flavors": null,
"toppings": {"Sweets": "$1","Fruit": "$2"}
},
{ "id": "2",
"name": "Million Favors Ice cream",
"description": "We have it all",
"flavors": { "Chocolate": "$0.39", "Lime": "$0.41" },
"toppings": {"Sweets": "$1","Fruit": "$2"}
},
{ "id": "3",
"name": "Hot Ice Cream",
"description": "Hottest Ice Cream in Town",
"flavors": null
}
]
public class Flavors {
public string Chocolate { get; set; }
public string Lime { get; set; }
}
public class Toppings{
public string Sweets { get; set; }
public string Fruit { get; set; }
}
public class MyArray {
public string id { get; set; }
public string name { get; set; }
public string description { get; set; }
public Flavors flavors { get; set; }
public Toppings toppings{ get; set; }
}
public class Root {
public List MyArray { get; set; }
}`
Apologies for asking a question within a question but I seem to be having an issue with the deserialize
when using
List array = JsonConvert.DeserializeObject<List>(IceCreamJsonData);
i get the following errorSeverity Code Description Project File Line Suppression State Error CS0305 Using the generic type 'List<T>' requires 1 type arguments
I think you have to say which is the type to deserialize
Thanks Alessandro,
All rocking and rolling now.