Azure反序列化JSON响应C#

Azure反序列化JSON响应C#,c#,C#,我尝试反序列化下面的JSON响应,但是当我打印每个参数的内容时,它显示错误 我的JSON响应: [ { "faceRectangle": { "top": 214, "left": 472, "width": 450, "height": 450 }, "faceAttributes": { "age": 19.0, "emotion": { "anger": 0.0, "contempt":

我尝试反序列化下面的JSON响应,但是当我打印每个参数的内容时,它显示错误

我的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
     }
  }
}
]
我可以使用以下命令反序列化我的代码:

public static async Task<List<RootObject>> MakeAnalysisRequest(string imageFilePath)
    {

        HttpClient client = new HttpClient();

        // Request headers.
        client.DefaultRequestHeaders.Add(
            "Ocp-Apim-Subscription-Key", subscriptionKey);

        // Request parameters. A third optional parameter is "details".
        string requestParameters = "&returnFaceId=false&returnFaceLandmarks=false&returnFaceAttributes=age,emotion";

        // Assemble the URI for the REST API Call.
        string uri = uriBase + "?" + requestParameters;

        HttpResponseMessage response;

        // Request body. Posts a locally stored JPEG image.
        byte[] byteData = GetImageAsByteArray(imageFilePath);

        using (ByteArrayContent content = new ByteArrayContent(byteData))
        {
            // This example uses content type "application/octet-stream".
            // The other content types you can use are "application/json"
            // and "multipart/form-data".
            content.Headers.ContentType =
                new MediaTypeHeaderValue("application/octet-stream");

            // Execute the REST API call.
            response = await client.PostAsync(uri, content);

            // Get the JSON response.
            string contentString = await response.Content.ReadAsStringAsync();
            // Display the JSON response.
            Console.WriteLine("\nResponse:\n");
            string format = JsonPrettyPrint(contentString);
            var deserializedJson = JsonConvert.DeserializeObject<List<RootObject>>(format);
            Console.WriteLine(format);
            Console.WriteLine(deserializedJson);
            Console.WriteLine("\nPress Enter to exit...");
            return deserializedJson;
        }
    }
我的错误代码如下所示:

索引超出范围。必须为非负数且小于集合的大小。 参数名称:索引

我该怎么解决呢? 谢谢

我的类对象:

class Program
{

    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; }
    }
请尝试以下代码:

var output1 = await MakeAnalysisRequest(imageFilePath);
if (output1 != null && output1.Count > 0)
{ 
    var x= output1[0].FaceAttributes.Emotion.Anger;
     Console.WriteLine(x);
}
if (output1 != null && output1.Count > 1)
{
    var y = output1[1].FaceAttributes.Emotion.Neutral;
}
存在超出范围的异常,因为反序列化的输出没有足够的元素可访问。请共享您的类对象以获取更多帮助

还要尽量避免使用
.Result
从asynch获取返回值,因为这可能导致死锁

提供课程后编辑

如果您的类是这样的,则相应地更改代码:

var output1 = await MakeAnalysisRequest(imageFilePath);
if (output1 != null && output1.Count > 0)
{ 
    var x= output1[0].FaceAttributes.Emotion.Anger;
    Console.WriteLine(x);
    var y = output1[0].FaceAttributes.Emotion.Neutral;
    Console.WriteLine(y);
}
jsons数组中只有一个
RootObject
,它由另外两个组成

Json数组
[]
对象
{}


一个包含两个对象的数组看起来像这样,而不是像这样,我的类是这样的,我编辑了这篇文章。当我使用这个语句“var Emo=await FaceEmotion.MakeAnalysisRequest(imageFilePath);”时。它仍然显示出错误是的,是的!非常感谢你。但我仍然对解释模糊不清。是因为我的JSON响应在[{{},{}}]中,所以我只有一个对象吗?所以我只能放数组[0]?就是这样!兄弟,非常感谢!
var output1 = await MakeAnalysisRequest(imageFilePath);
if (output1 != null && output1.Count > 0)
{ 
    var x= output1[0].FaceAttributes.Emotion.Anger;
    Console.WriteLine(x);
    var y = output1[0].FaceAttributes.Emotion.Neutral;
    Console.WriteLine(y);
}