亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

反序列化問題

反序列化問題

C#
慕哥6287543 2023-07-09 17:38:42
我必須制作一個使用照片返回年齡的程序,我正在使用 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? ? ? ? ?},我更改了代碼以減少它,因為我只需要年齡?,F在輸出是這樣的[{"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);有人可以幫我解決這個問題嗎?提前致謝。
查看完整描述

1 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

您可以使用該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-cd73c6dec644

162

0

查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 113 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號