提取 JSON 数据 Xamarin

Posted

技术标签:

【中文标题】提取 JSON 数据 Xamarin【英文标题】:Extracting JSON data Xamarin 【发布时间】:2018-07-21 20:46:48 【问题描述】:

我正在使用一个使用 Azure 人脸 API 的 Xamarin.Forms 应用程序。使用此 API,您可以检索 JSON 响应(见下文)。

我想提取图像中人的性别,但由于我对此很陌生,所以遇到了麻烦。

我将完整的 JSON 响应提取到一个字符串中,但我希望能够提取图像中人物的“性别”或“年龄”等数据。

["faceId":"9448dfe4-afb6-4557-94fe-010fc439ff36","faceRectangle":"top":635,"left":639,"width":789,"height":789,"faceAttributes":"smile":0.187,"headPose":"pitch":0.0,"roll":-1.6,"yaw":-7.9,"gender":"male","age":34.6,"facialHair":"moustache":0.5,"beard":0.6,"sideburns":0.6,"glasses":"NoGlasses","emotion":"anger":0.0,"contempt":0.69,"disgust":0.0,"fear":0.0,"happiness":0.187,"neutral":0.12,"sadness":0.002,"surprise":0.0,"blur":"blurLevel":"low","value":0.15,"exposure":"exposureLevel":"overExposure","value":0.85,"noise":"noiseLevel":"medium","value":0.42,"makeup":"eyeMakeup":false,"lipMakeup":false,"accessories":[],"occlusion":"foreheadOccluded":false,"eyeOccluded":false,"mouthOccluded":false,"hair":"bald":0.02,"invisible":false,"hairColor":["color":"brown","confidence":1.0,"color":"black","confidence":0.95,"color":"other","confidence":0.22,"color":"blond","confidence":0.11,"color":"gray","confidence":0.05,"color":"red","confidence":0.04]]

这就是我将 JSON 数据设置为字符串的方式。

 string contentString = await response.Content.ReadAsStringAsync();

【问题讨论】:

【参考方案1】:

您通常希望创建一个代表您的响应的类 e. G。 Person。然后你可以使用Newtonsoft.Json nuget 包来反序列化对象。:

var person = JsonConvert.DeserializeObject<Person>(contentString);

现在您可以访问以下值:

person.Gender

【讨论】:

【参考方案2】:

使用 Newtonsoft.Json:

var results = JsonConvert.DeserializeObject<AzureFace>(contentString);
Debug.WriteLine(results.faceAttributes.age);

使用的 C# 模型(通过http://jsonutils.com)

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 IList<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 IList<object> accessories  get; set; 
    public Occlusion occlusion  get; set; 
    public Hair hair  get; set; 


public class AzureFace 

    public string faceId  get; set; 
    public FaceRectangle faceRectangle  get; set; 
    public FaceAttributes faceAttributes  get; set; 

【讨论】:

您好,感谢您的精彩回复!我现在有另一个问题,在 JSON 响应中我没有数组标头值(EG:“employees”:[..........) @JohnDoe 不确定我是否理解,employees 与 Azure Face API 有什么关系?我建议用代码/问题发布另一个问题 谢谢@sushihangover!我在这里更新了问题:***.com/questions/48750774/…

以上是关于提取 JSON 数据 Xamarin的主要内容,如果未能解决你的问题,请参考以下文章

怎么提取json里的数据变成EXCEL?

jmeter提取json数据进行接口参数关联

提取 JSON 数据 Xamarin

从数据中提取 JSON 数组

JSON数据提取

如何从PostgreSQL json中提取数组