从 JSON 数组中提取头发颜色

Posted

技术标签:

【中文标题】从 JSON 数组中提取头发颜色【英文标题】:Extracting hair color from JSON array 【发布时间】:2018-07-31 08:03:36 【问题描述】:

我遇到了一个小问题,即从 JSON 数据中提取人物头发的第一种颜色。我对此很陌生,而且收效甚微。请帮忙

关于如何获得头发颜色并将其设置为字符串的任何想法?

["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]]

这就是我尝试访问颜色的方式,但无济于事

string x = myObj.faceAttributes.hair.hairColor.ToString();

这是我回来的:

Debug Result: System.Collections.Generic.List`1[Namespace.Model.HairColor]

以下是课程:

  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; 
    

请帮忙!

【问题讨论】:

【参考方案1】:

hairColor 是一个数组,所以你必须指定你想要的元素。

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

【讨论】:

如果您觉得有帮助,请接受答案(绿色复选标记),谢谢!【参考方案2】:

如果您查看类结构,您会发现 Hair 包含一个头发颜色列表。调试消息也告诉你同样的事情;您在 List 上调用 ToString()。

相反,正确的方法是单独或以逗号分隔值打印每种头发颜色。在不确切知道您想如何输出/使用头发颜色的情况下尝试以下方法之一。

逗号分隔:这将产生一串逗号分隔的头发颜色

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

每个人:

foreach (string x in myObj.faceAttributes.hair.hairColor)

    System.Out.WriteLine(x);
    // Or whatever else you would like to do with this.

【讨论】:

以上是关于从 JSON 数组中提取头发颜色的主要内容,如果未能解决你的问题,请参考以下文章

提取数组中UIImageview的背景颜色

更换口红颜色,眼睛颜色(美瞳),头发颜色

Swiftui:从 JSON 中获取动态颜色渐变?

如何在我的 CSS 中使用存储在 JSON 中的颜色?

简单的卷云怎么涂颜色像懒羊羊的头发一样

使用 cvtColor 转换单个 BGR 颜色并使用结果从图像中提取该颜色