反序列化问题

Problems with Deserializing

我必须制作一个程序 return 使用照片的年龄,我正在使用 Azure 的 faceApi 服务。我一直在尝试使用此处描述的代码 https://docs.microsoft.com/es-es/azure/cognitive-services/face/quickstarts/csharp,其中 return 下面的代码

[
   {
      "faceId": "f7eda569-4603-44b4-8add-cd73c6dec644",
      "faceRectangle": {
         "top": 131,
         "left": 177,
         "width": 162,
         "height": 162
      },
      "faceAttributes": {
         "smile": 0.0,
         "headPose": {
            "pitch": 0.0,
            "roll": 0.1,
            "yaw": -32.9
         },
         "gender": "female",
         "age": 22.9,
         "facialHair": {
            "moustache": 0.0,
            "beard": 0.0,
            "sideburns": 0.0
         },
         "glasses": "NoGlasses",
         "emotion": {
            "anger": 0.0,
            "contempt": 0.0,
            "disgust": 0.0,
            "fear": 0.0,
            "happiness": 0.0,
            "neutral": 0.986,
            "sadness": 0.009,
            "surprise": 0.005
         },
         "blur": {
            "blurLevel": "low",
            "value": 0.06
         },
         "exposure": {
            "exposureLevel": "goodExposure",
            "value": 0.67
         },
         "noise": {
            "noiseLevel": "low",
            "value": 0.0
         },
         "makeup": {
            "eyeMakeup": true,
            "lipMakeup": true
         },
         "accessories": [

         ],
         "occlusion": {
            "foreheadOccluded": false,
            "eyeOccluded": false,
            "mouthOccluded": false
         },
         "hair": {
            "bald": 0.0,
            "invisible": false,
            "hairColor": [
               {
                  "color": "brown",
                  "confidence": 1.0
               },
               {
                  "color": "black",
                  "confidence": 0.87
               },
               {
                  "color": "other",
                  "confidence": 0.51
               },
               {
                  "color": "blond",
                  "confidence": 0.08
               },
               {
                  "color": "red",
                  "confidence": 0.08
               },
               {
                  "color": "gray",
                  "confidence": 0.02
               }
            ]
         }
      }
   }
]

我更改了代码以减少它,因为我只需要年龄。现在输出是这样的

[{"faceId":"b2342836-6d99-4f69-b656-1a64b786b421","faceRectangle":{"top":60,"left":52,"width":58,"height":58},"faceAttributes":{"age":40.0}}] 

然后,我只需要反序列化并获取年龄,为此我创建了这个 class

public class Analysis
{
    private string faceId;
    private FaceRectangle fRectangles;
    private faceAttributes fAttributes;

    public Analysis(string faceId, FaceRectangle fRectangles, faceAttributes fAttributes)
    {
        this.faceId = faceId;
        this.fRectangles = fRectangles;
        this.fAttributes = fAttributes;
    }

    public string FaceId { get => faceId; set => faceId = value; }
    public FaceRectangle FRectangles { get => fRectangles; set => fRectangles = value; }
    public faceAttributes FAttributes { get => fAttributes; set => fAttributes = value; }
}

public class faceAttributes
{
    private double age;

    public faceAttributes(double age)
    {
        this.Age = age;
    }

    public double Age { get => age; set => age = value; }
}

public class FaceRectangle
{
    private int top;
    private int left;
    private int width;
    private int height;

    public FaceRectangle(int top, int left, int width, int height)
    {
        this.Top = top;
        this.Left = left;
        this.Width = width;
        this.Height = height;
    }

    public int Top { get => top; set => top = value; }
    public int Left { get => left; set => left = value; }
    public int Width { get => width; set => width = value; }
    public int Height { get => height; set => height = value; }
}

我认为这一切都是正确的,但任何时候我试图反序列化 return 一个空对象。例如我试过使用这个

Analysis dsjson = new JavaScriptSerializer().Deserialize<Analysis>(contentString);

有人可以帮我解决这个问题吗?。

提前致谢。

您可以使用 Newtonsoft 库,这是一个流行的 high-performance JSON .NET 框架。您可以阅读更多相关信息 here

关于您的 JSON 字符串,使用此包将产生您想要的结果。请看下面由 Model 类 和反序列化过程组成的代码:

using System;
using Newtonsoft.Json;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string json=@"[{'faceId':'f7eda569-4603-44b4-8add-cd73c6dec644','faceRectangle':{'top':131,'left':177,'width':162,'height':162},'faceAttributes':{'smile':0,'headPose':{'pitch':0,'roll':0.1,'yaw':-32.9},'gender':'female','age':22.9,'facialHair':{'moustache':0,'beard':0,'sideburns':0},'glasses':'NoGlasses','emotion':{'anger':0,'contempt':0,'disgust':0,'fear':0,'happiness':0,'neutral':0.986,'sadness':0.009,'surprise':0.005},'blur':{'blurLevel':'low','value':0.06},'exposure':{'exposureLevel':'goodExposure','value':0.67},'noise':{'noiseLevel':'low','value':0},'makeup':{'eyeMakeup':true,'lipMakeup':true},'accessories':[],'occlusion':{'foreheadOccluded':false,'eyeOccluded':false,'mouthOccluded':false},'hair':{'bald':0,'invisible':false,'hairColor':[{'color':'brown','confidence':1},{'color':'black','confidence':0.87},{'color':'other','confidence':0.51},{'color':'blond','confidence':0.08},{'color':'red','confidence':0.08},{'color':'gray','confidence':0.02}]}}}]";
        var Sresponse = JsonConvert.DeserializeObject<List<RootObject>>(json);

        foreach(var result in Sresponse)
        {
            //Get your data here from the deserialization
            Console.WriteLine(result.faceId);
            Console.WriteLine(result.faceRectangle.height);
            Console.WriteLine(result.faceAttributes.emotion.disgust);           
        }

    }
}

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 RootObject
{
    public string faceId { get; set; }
    public FaceRectangle faceRectangle { get; set; }
    public FaceAttributes faceAttributes { get; set; }
}

输出:

f7eda569-4603-44b4-8add-cd73c6dec644
162
0

工作演示:https://dotnetfiddle.net/EWZYwA