Extracting hair color from JSON array Extracting hair color from JSON array json json

Extracting hair color from JSON array


hairColor is an array, so you have to specify which element you want.

string x = myObj.faceAttributes.hair.hairColor[0].color;


If you have a look at the class structure, you will see that Hair contains a List of Hair Colour. The debug message is also telling you the same thing; that you are calling ToString() on a List.

Instead the correct approach would be to print out each of the Hair colours, either separately or as a comma separated value. Without knowing exactly how you want to output/use the hair colour(s) try one of the approaches below.

Comma Separated: This will produce a string of comma separated hair colours

string x = string.Join(",", myObj.faceAttributes.hair.hairColor);

For Each:

foreach (string x in myObj.faceAttributes.hair.hairColor){    System.Out.WriteLine(x);    // Or whatever else you would like to do with this.}