I am stuck with a small problem of extracting the first color of the persons hair from JSON data. I am very new to this and am having little success with it. Please help
Any ideas as to how I can get the color of the hair out and setting it to a string?
[{"faceId":"b5472f5d-f6f2-41bd-9c52-cb585372108c","faceRectangle":{"top":1005,"left":786,"width":864,"height":864},"faceAttributes":{"smile":0.185,"headPose":{"pitch":0.0,"roll":-7.6,"yaw":9.2},"gender":"male","age":41.8,"facialHair":{"moustache":0.8,"beard":1.0,"sideburns":0.9},"glasses":"NoGlasses","emotion":{"anger":0.004,"contempt":0.012,"disgust":0.006,"fear":0.057,"happiness":0.185,"neutral":0.516,"sadness":0.043,"surprise":0.177},"blur":{"blurLevel":"medium","value":0.32},"exposure":{"exposureLevel":"goodExposure","value":0.6},"noise":{"noiseLevel":"high","value":1.0},"makeup":{"eyeMakeup":false,"lipMakeup":false},"accessories":[],"occlusion":{"foreheadOccluded":false,"eyeOccluded":false,"mouthOccluded":false},"hair":{"bald":0.05,"invisible":false,"hairColor":[{"color":"black","confidence":0.97},{"color":"brown","confidence":0.89},{"color":"other","confidence":0.41},{"color":"gray","confidence":0.22},{"color":"blond","confidence":0.17},{"color":"red","confidence":0.09}]}}}]
This is how I am trying to access the color but to no avail
string x = myObj.faceAttributes.hair.hairColor.ToString();
This is what I get back:
Debug Result: System.Collections.Generic.List`1[Namespace.Model.HairColor]
Here are the classes:
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class HeadPose
{
public double pitch { get; set; }
public double roll { get; set; }
public double yaw { get; set; }
}
public class FacialHair
{
public double moustache { get; set; }
public double beard { get; set; }
public double sideburns { get; set; }
}
public class Emotion
{
public double anger { get; set; }
public double contempt { get; set; }
public double disgust { get; set; }
public double fear { get; set; }
public double happiness { get; set; }
public double neutral { get; set; }
public double sadness { get; set; }
public double surprise { get; set; }
}
public class Blur
{
public string blurLevel { get; set; }
public double value { get; set; }
}
public class Exposure
{
public string exposureLevel { get; set; }
public double value { get; set; }
}
public class Noise
{
public string noiseLevel { get; set; }
public double value { get; set; }
}
public class Makeup
{
public bool eyeMakeup { get; set; }
public bool lipMakeup { get; set; }
}
public class Occlusion
{
public bool foreheadOccluded { get; set; }
public bool eyeOccluded { get; set; }
public bool mouthOccluded { get; set; }
}
public class HairColor
{
public string color { get; set; }
public double confidence { get; set; }
}
public class Hair
{
public double bald { get; set; }
public bool invisible { get; set; }
public List<HairColor> hairColor { get; set; }
}
public class FaceAttributes
{
public double smile { get; set; }
public HeadPose headPose { get; set; }
public string gender { get; set; }
public double age { get; set; }
public FacialHair facialHair { get; set; }
public string glasses { get; set; }
public Emotion emotion { get; set; }
public Blur blur { get; set; }
public Exposure exposure { get; set; }
public Noise noise { get; set; }
public Makeup makeup { get; set; }
public List<object> accessories { get; set; }
public Occlusion occlusion { get; set; }
public Hair hair { get; set; }
}
public class Face
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
}
Please help!
hairColor是一个数组,因此您必须指定所需的元素。
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.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.