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

I understand What I wanted is simple but looks a bit more complex for me. I have use the

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

to connect and get the response and I have passed in the contentString which is the JSON data. I just want to get out my desired value. Using description as an example will be more helpful. Thanks

recnac
  • 3,744
  • 6
  • 24
  • 46
  • There's pre-existing NuGet package that interfaces with Congnitive Services: [Microsoft.ProjectOxford.Vision](https://www.nuget.org/packages/Microsoft.ProjectOxford.Vision/), perhaps there's no point in reinventing the wheel? – orhtej2 Jul 16 '18 at 07:53
  • 1
    Is that not a dupe of every Json to c# question? – Drag and Drop Jul 16 '18 at 08:24
  • 1
    Possible duplicate of [How to get some values from a JSON string in C#?](https://stackoverflow.com/questions/17617594/how-to-get-some-values-from-a-json-string-in-c) – BugFinder Jul 16 '18 at 08:33

4 Answers4

2

Complementing Andrew's answer, you can use the following class structure:

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

And then use Newsontsoft's Result result = JsonConvert.DeserializeObject<Result>(json); to deserialize your Json.

Newtonsoft download: https://www.nuget.org/packages/Newtonsoft.Json/

Nathalia Soragge
  • 1,415
  • 6
  • 21
  • Hello, i created the class but how do i access each values in the variable as its turns out to be an array. e.g i need get the first value in description and the text in caption Result rsult = JsonConvert.DeserializeObject(contentString); var value1= rsult.description; – Oluwatosin Samuel Babalola Jul 16 '18 at 14:09
  • Nice! If you only need one specific value from the Json, you can also take a look in `SelectToken` ([link](https://www.newtonsoft.com/json/help/html/SelectToken.htm)), as pointed out by Mahesh. – Nathalia Soragge Jul 16 '18 at 18:04
1

The best option here would be to create a model that will represent the response and then deserialize the response into an instance of the model using JsonConvert.DeserializeObject of Newtonsoft Json.Net. It will be much more OOP-like approach, easier to maintain and extend.

See an example here.

Andrew K
  • 263
  • 1
  • 8
0

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

Check the JToken class in details. Everything you can extract either using whole class like structure using deserialization or extract any specific value directly

Mahesh Malpani
  • 1,782
  • 16
  • 27
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/