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

标签 c# json azure deserialization

我的 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/

相关文章:

c# - 数据集的简单线性回归

c# - 如何在c#中测试条件运算符的右结合性

arrays - 尝试从 SwiftUI View 中的嵌套数组调用

azure - 将 Live ID 中的 Azure ACS 名称标识符与已知用户列表相匹配吗?

c# - 在数据库中插入空值

c# - 检查 RichTextBox ScrollBar thumb 是否在滚动条的底部

java - Json 到 kotlin 数据类

javascript - 在 javascript 中迭代 json 哈希

azure - 如何使用 ASP.Net (C#) 在 Azure Active Directory 中创建组/用户?

php - 适用于 PHP 的 Azure Cloudstorage - 调用函数 createBlobService() 时出错