2

I tried so many things but i am unable to get the section under objects out

API Output

"objects": [
  {
    "date": "Mon, 11 Sep 2017 00:00:00 GMT",
    "images": [
      {
        "naturalHeight": 298,
        "width": 810,
        "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_c.png",
        "naturalWidth": 810,
        "primary": true,
        "height": 298
      },
      {
        "naturalHeight": 393,
        "width": 300,                    
        "title": "8 Ways to Enter",
        "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_csss.png",
        "naturalWidth": 563,
        "height": 223
      },
      {
        "naturalHeight": 300,
        "width": 169,                    
        "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_themec.png",
        "naturalWidth": 169,
        "height": 297
      },
      {
        "naturalHeight": 300,
        "width": 169,                  
        "title": "Before: Android Oreo’s Stock, Light UI",
        "url": "https://www1.google.com/files/2017/09/Feature-Image-810x298_dec.png",
        "naturalWidth": 169,
        "height": 297
      }, ...... continue

I need to get the first image in which the PRIMARY = TRUE
I tried the following with no luck

var dirs = JObject.Parse(json)
            .Descendants()
            .Where(x=>x is JObject)
            .Where(x=>x["primary"]!=null && x["url"]!=null)
            .Select(x =>new { URL= (string)x["primary"], PRIMARY = (string)x["url"] })
            .ToList();

var id = dirs.Find(x => x.Primary == "true").URL;
1
  • 2
    deserialize the json using Newtonsoft.json to runtime objects. Then you can access the properties Commented Sep 13, 2017 at 16:57

2 Answers 2

2

Your code would look like

var jObj = JObject.Parse(json)["objects"]
            .SelectMany(x => x["images"])
            .Where(x => x["primary"] != null)
            .FirstOrDefault(x => (bool)x["primary"]);

You can go one step further and by declaring a class like

public class Image
{
    public int naturalHeight { get; set; }
    public int width { get; set; }
    public string url { get; set; }
    public int naturalWidth { get; set; }
    public bool primary { get; set; }
    public int height { get; set; }
    public string title { get; set; }
}

You can write

var image = jObj.ToObject<Image>();
Sign up to request clarification or add additional context in comments.

Comments

-1

You can use Newtonsoft.Json NuGet package and use as its well.

Example shown on base of your Json.

    public class SampleJson
    {
        [JsonProperty("date")]
        public string Date { get; set; }
        [JsonProperty("images")]
        public List<Image> Images { get; set; }
        // nested type, you can write it out of SampleJson object brackets
        class Image
        {
             [JsonProperty("naturalHeight")]
             public int NaturalHeight { get; set; }
             [JsonProperty("width")]
             public int Width { get; set; }
             [JsonProperty("url")]
             public string Url { get; set; }
             [JsonProperty("naturalWidth")]
             public int NaturalWidth { get; set; }
             [JsonProperty("primary")]
             public bool Primary { get; set; }
             [JsonProperty("height")]
             public int Height { get; set; }
             [JsonProperty("title")]
             public string Title { get; set; }
        }
    }

And the usage of this method.

var json = HttpClinet.GetAsync("URL").Result; // your json

SampleJson obj = JsonConvert.DeserializeObject<SampleJson>(json);
var query = obj.Images.FirstOrDefault(); // Use LINQ to C# objects

Hope it's helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.