反序列化问题

我必须制作一个使用照片返回年龄的程序,我正在使用 Azure 的faceApi 服务。

它返回下面的代码

[

   {

      "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

         },




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


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

我认为一切都是正确的,但是每当我尝试反序列化时都会返回一个对象 null。例如我尝试过使用这个

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

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

提前致谢。


慕哥6287543
浏览 71回答 1
1回答

月关宝盒

您可以使用该Newtonsoft库,它是 .NET 的流行高性能 JSON 框架。您可以在这里阅读更多相关信息关于您的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-cd73c6dec6441620
打开App,查看更多内容
随时随地看视频慕课网APP