gpt4 book ai didi

c# - Azure JSON 响应反序列化 C#

转载 作者:行者123 更新时间:2023-12-02 04:30:22 25 4
gpt4 key购买 nike

我的 JSON 响应如下所示,

[
{
"faceRectangle": {
"top": 214,
"left": 472,
"width": 450,
"height": 450
},
"faceAttributes": {
"age": 19.0,
"emotion": {
"anger": 0.0,
"contempt": 0.0,
"disgust": 0.0,
"fear": 0.0,
"happiness": 0.0,
"neutral": 0.996,
"sadness": 0.003,
"surprise": 0.001
}
}
}
]

我的C#类如下:

public class Output
{
public List<FaceRectangle> FaceRectangles { get; set; }
public List<FaceAttribute> faceAttributes { get; set; }
}
public class FaceAttribute
{
public List<Emotion> Emotions { get; set; }
public int age { get; set; }
}
public class Emotion {
public float anger { get; set; }
public float contempt { get; set; }
public float disgust { get; set; }
public float fear { get; set; }
public float happiness { get; set; }
public float neutral { get; set; }
public float sadness { get; set; }
public float surprise { get; set; }
}
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}

但是,当我尝试反序列化上述响应时,出现以下错误。错误代码显示为“不支持数组的反序列化” enter image description here

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

最佳答案

尝试使用从 json2csharp 创建的这些类:

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 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 FaceAttributes
{
public double Age { get; set; }
public Emotion Emotion { get; set; }
}

public class RootObject
{
public FaceRectangle FaceRectangle { get; set; }
public FaceAttributes FaceAttributes { get; set; }
}

然后您可以将 JSON 数组响应反序列化为 List<RootObject使用Newtonsoft.Json NuGet 包:

using Newtonsoft.Json;

...

var json = @"[{""faceRectangle"": {""top"": 214,""left"": 472,""width"": 450,""height"": 450},""faceAttributes"": {""age"": 19.0,""emotion"": {""anger"": 0.0,""contempt"": 0.0,""disgust"": 0.0,""fear"": 0.0,""happiness"": 0.0,""neutral"": 0.996,""sadness"": 0.003,""surprise"": 0.001}}}]";

var deserializedJson = JsonConvert.DeserializeObject<List<RootObject>>(json);

关于c# - Azure JSON 响应反序列化 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59978547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com