如何反序列化 JSON 并在 C# 中获取值

// Get the JSON response.

                    string contentString = await response.Content.ReadAsStringAsync();


                    Console.WriteLine(contentString);

                    var rs = Newtonsoft.Json.Linq.JToken.Parse(contentString);

 Result rst = JsonConvert.DeserializeObject<Result>(contentString);


//Here i need to get the first value in the description as it appears to be a list


var firstValue= rst.description;


//And also a value from caption

var captionValue = rst.Caption



 public class Result

    {

        public Category[] categories { get; set; }

        public Description description { get; set; }

        public string requestId { get; set; }

        public  Caption caption { get; set;}

        public Metadata metadata { get; set; }

        public Color color { get; set; }


    }


    public class Description

    {

        public string[] tags { get; set; }

        public Caption[] captions { get; set; }

    }


    public class Caption

    {

        public string text { get; set; }

        public float confidence { get; set; }

    }


    public class Metadata

    {

        public int width { get; set; }

        public int height { get; set; }

        public string format { get; set; }

    }


    public class Color

    {

        public string dominantColorForeground { get; set; }

        public string dominantColorBackground { get; set; }

        public string[] dominantColors { get; set; }

        public string accentColor { get; set; }

        public bool isBWImg { get; set; }

    }


    public class Category

    {

        public string name { get; set; }

        public float score { get; set; }

    }


我明白我想要的很简单,但对我来说看起来有点复杂。我已经使用了


Result //rst = JsonConvert.DeserializeObject<Result>(contentString);

连接并获取响应,我已经传入了 JSON 数据的 contentString。我只想得到我想要的价值。使用 description 作为示例会更有帮助。谢谢


开心每一天1111
浏览 268回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP