I have the following Json:
[
{
"id": 3707571,
"name": "Gekochte Eier in Senfsauce, dazu Kartoffeln - oder Kartoffelpüree",
"category": "Angebot 1",
"prices": {
"students": 1.4,
"employees": 3.1,
"pupils": null,
"others": 3.1
},
"notes": [
"Vegetarisch"
]
},
{
"id": 3709978,
"name": "Currywurst mit hausgemachter Currysauce und Pommes frites, dazu bunter Salat",
"category": "Angebot 2",
"prices": {
"students": 2,
"employees": 3.9,
"pupils": null,
"others": 3.9
},
"notes": [
"Schweinefleisch"
]
}
]
When I remove the Price, the deserialization works fine and I can convert it to an Object using Json.NET in C#.
But with the price included I get a error message saying it can only be deserialize using a Json Array.
The object I used to deserialize it:
namespace TelegramBot
{
class Angebot
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("category")]
public string category { get; set; }
[JsonProperty("prices")]
public List<float> prices { get; set; }
[JsonProperty("notes")]
public IList<string> notes { get; set; }
}
}
How can I deserialize the prices into a list of floats (or other datatype) in C#?
Edit:
Like suggested I changed the price property to an array. The error still occurs.
The error message is:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Single[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
To deserialize the Json I use following command:
IList<Angebot> angebotsListe = JsonConvert.DeserializeObject<List<Angebot>>(mealsInformation);
mealsInformation is the Json String.
"prices"object fixed or variable?[2, 3.9, 3.1]and all should be good. NB: You also can't use aNULLgiven you're usingfloatrather thanfloat?.