0

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.

4
  • 2
    Read the error. You need to deserialize it into an array. Commented Oct 15, 2018 at 17:05
  • 4
    Make another class for price with all its attributes . Then use it instead of List<float> Commented Oct 15, 2018 at 17:06
  • Are the set of possible property names for the "prices" object fixed or variable? Commented Oct 15, 2018 at 17:06
  • 2
    Your prices property is declared as a float, but the values are held as an object with several name/value pairs. Change the prices json to [2, 3.9, 3.1] and all should be good. NB: You also can't use a NULL given you're using float rather than float?. Commented Oct 15, 2018 at 17:07

2 Answers 2

2

This is the JSON related to PRICES:

"prices": {
  "students": 1.4,
  "employees": 3.1,
  "pupils": null,
  "others": 3.1
},

This is the code for holding that data:

[JsonProperty("prices")]
public List<float> prices { get; set; }

The JSON definition is a single object with properties students, employees, pupils, others. The C# definition is an array of (non-nullable) floats.

Solutions

There are 3 ways to fix this:

1. Change your C# to match your JSON:

[JsonProperty("prices")]
public Price prices { get; set; }

//...

class Price 
{
    [JsonProperty("students")]
    public float? Students{get;set;}
    [JsonProperty("employees")]
    public float? Employees{get;set;}
    [JsonProperty("pupils")]
    public float? Pupils{get;set;}
    [JsonProperty("prices")]
    public float? Others{get;set;}
    //...
}

2. Change your JSON to match your C#

"prices": [
  1.4,
  3.1,
  3.1
],

3. Change both

JSON:

"prices": [
    {"name":"students","value":1.4},
    {"name":"employees","value":3.1},
    {"name":"pupils","value":null},
    {"name":"others","value":3.1}
 ],

C#:

[JsonProperty("prices")]
public List<Price> prices { get; set; }

//...

class Price 
{
    [JsonProperty("name")]
    public string Name {get;set;}
    [JsonProperty("value")]
    public float? Value {get;set;}
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

2

Declare a proper class

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 Prices Prices { get; set; }

        [JsonProperty("notes")]
        public IList<string> notes { get; set; }
    }
}

namespace TelegramBot
{
    class Prices
    {
        [JsonProperty("students")]
        public float? students { get; set; }

        [JsonProperty("employees")]
        public float? employees { get; set; }

        [JsonProperty("pupils")]
        public float? pupils { get; set; }

        [JsonProperty("others")]
        public float? others { get; set; }

    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.