0

I'm trying to read the data in a Json, but for some reason I can't find my mistake. This is an example of what I'm looking for.

{
   "number": 1,
   "pencil":
   {
      "Array": [
           {
               "color":
               {
                  "red": 0,
                  "green": 0,
                  "blue": 0
               },
               "id": 1234
           },
           {
               "color":
               {
                  "red": 100,
                  "green": 10,
                  "blue": 50
               },
               "id": 1235
           },
       ]
   },
   "something_else": 2
}

I tried this line but it keeps doing error.

var test = JsonConvert.DeserializeObject<List<Pencil>>(jsonString);

I am trying to get the red, green blue value and the id, but I don't get how.

public class Pencil
{
   public List<Color> colors {get; set;}
   public int id;
}

public class Color
{
   public int red;
   public int green;
   public int blue;
}
Raskelot
  • 1
  • 1
  • You need to edit your question to include your `pencil` class and the text of the error you get. – stuartd Feb 05 '20 at 15:57
  • 1
    updated, sorry I forgot – Raskelot Feb 05 '20 at 15:59
  • This json is not valid for this pencil class – Selim Yildiz Feb 05 '20 at 15:59
  • Your array is a list of objects with both Id and Color. Your class list is only of colors – Greggz Feb 05 '20 at 16:00
  • The *array* is named `Array` in the json file and `colors` in the C# objects, you must use some `JsonPropertyAttribute` to fix that: https://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net – Orace Feb 05 '20 at 16:04

1 Answers1

5

Firstly, you should have root object:

public class Root
{
    public int number {get; set;}
    public Pencil pencil {get; set;}
}

Secondly, your property with colors should be named Array:

public class Pencil
{
   public List<Color> array {get; set;}
   public int id;
}

Thirdly, you can't deserialize to Color, as in json pencil contains id property and color object. You should have something like wrapper:

public class ColorInfo
{
    public int id {get; set;}
    public Color color {get; set;}
}

so your Pencil:

public class Pencil
{
   public int id;
   public List<ColorInfo> array {get; set;} // ColorInfo instead of Color
}

Finally, deserialize:

var result = JsonConvert.DeserializeObject<Root>(jsonString);
Roman
  • 11,966
  • 10
  • 38
  • 47
  • You also can use `JsonPropertyAttribute` to specify json element name. – Orace Feb 05 '20 at 16:04
  • Thanks I think I get it now! Yeah the root class was probably it! – Raskelot Feb 05 '20 at 16:05
  • @Orace, yes, thanks. But anyway the `Color` array is inside `Pencil` and json has different structure. – Roman Feb 05 '20 at 16:08
  • @RomanDoskoch, you wrote: "Secondly, your property with colors should be named Array", this is a breaking change, while it can stay named `colors` if you add a `JsonPropertyAttribute` over it. – Orace Feb 05 '20 at 16:14
  • @Orace, why is it breaking change? In code it is much more easy to fix name (you know references). In case if there is a lot of properties with different name in json comparing to `c#` object will you set the attribute for all these not matching properties? – Roman Feb 05 '20 at 16:18
  • It's a breaking change because (by definition of a breaking change) you have to modify all the code that refer to this class. The json name "array" is a poor choice for a property, I will prefer to fix the json, but maybe it's not possible. I just want to put forward that your can avoid poor json item name in your code. – Orace Feb 05 '20 at 16:23