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;
}
5
  • You need to edit your question to include your pencil class and the text of the error you get. Commented Feb 5, 2020 at 15:57
  • 1
    updated, sorry I forgot Commented Feb 5, 2020 at 15:59
  • This json is not valid for this pencil class Commented Feb 5, 2020 at 15:59
  • Your array is a list of objects with both Id and Color. Your class list is only of colors Commented Feb 5, 2020 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: stackoverflow.com/questions/8796618/… Commented Feb 5, 2020 at 16:04

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

You also can use JsonPropertyAttribute to specify json element name.
Thanks I think I get it now! Yeah the root class was probably it!
@Orace, yes, thanks. But anyway the Color array is inside Pencil and json has different structure.
@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, 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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.