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

时间:2018-07-16 07:49:18

标签: c# json

// 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; }
    }
} 

JSON DATA:

{
   "categories": [
      {
         "name": "abstract_",
         "score": 0.00390625
      },
      {
         "name": "others_",
         "score": 0.0234375
      },
      {
         "name": "outdoor_",
         "score": 0.00390625
      }
   ],
   "description": {
      "tags": [
         "road",
         "building",
         "outdoor",
         "street",
         "night",
         "black",
         "city",
         "white",
         "light",
         "sitting",
         "riding",
         "man",
         "side",
         "empty",
         "rain",
         "corner",
         "traffic",
         "lit",
         "hydrant",
         "stop",
         "board",
         "parked",
         "bus",
         "tall"
      ],
      "captions": [
         {
            "text": "a close up of an empty city street at night",
            "confidence": 0.7965622853462756
         }
      ]
   },
   "requestId": "dddf1ac9-7e66-4c47-bdef-222f3fe5aa23",
   "metadata": {
      "width": 3733,
      "height": 1986,
      "format": "Jpeg"
   },
   "color": {
      "dominantColorForeground": "Black",
      "dominantColorBackground": "Black",
      "dominantColors": [
         "Black",
         "Grey"
      ],
      "accentColor": "666666",
      "isBWImg": true
   }
}

我了解我想要的很简单,但对我来说看起来却有些复杂。我已经使用

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

以连接并获取响应,我已经传递了contentString这是JSON数据。我只想拿出我想要的价值。以描述为例会更有帮助。谢谢

4 个答案:

答案 0 :(得分:2)

作为安德鲁的答案的补充,您可以使用以下类结构:

    public class Result
    {
        public Category[] categories { get; set; }
        public Description description { get; set; }
        public string requestId { 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; }
    }

然后使用Newsontsoft的Result result = JsonConvert.DeserializeObject<Result>(json);反序列化您的Json。

Newtonsoft下载:https://www.nuget.org/packages/Newtonsoft.Json/

答案 1 :(得分:1)

这里最好的选择是创建一个表示响应的模型,然后使用Newtonsoft Json.Net的JsonConvert.DeserializeObject将响应反序列化为模型的实例。它将更像OOP,更易于维护和扩展。

请参见示例here

答案 2 :(得分:0)

https://www.newtonsoft.com/json/help/html/SelectToken.htm

详细检查JToken类。您可以使用像反序列化这样的整个类结构来提取所有内容,也可以直接提取任何特定值

答案 3 :(得分:0)

To get the caption under description, first generate a class with the JSON response. You can use the link to do that [json2c#][1]

//Using the result class

// Get the JSON response.
  string contentString = await response.Content.ReadAsStringAsync();
  Result rsult = JsonConvert.DeserializeObject<Result>(contentString);
  var dsc = rsult.description;
  string cap = dsc.captions[0].text.ToString();

public class Result
    {
        public Category[] categories { get; set; }
        public Description description { get; set; }
        public string requestId { 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; }

  [1]: http://json2csharp.com/