Komicon
Komicon

Reputation: 11

How to display JSON values from API

I would like to display this API content e.g.

Console.WriteLine(data.Title)

which would display

"Lembethe leading by example"

Have a look at my code

using (var webClient = new System.Net.WebClient())
{
    var json = webClient.DownloadString(@"url");
    var data = JsonConvert.DeserializeObject<Result>(json);
    Console.WriteLine(data.Title);
}

These are my Classes below 1

public partial class Result
{
    [JsonProperty("tags")]
    public List<Tag> Tags { get; set; }

    [JsonProperty("custom_tags")]
    public List<string> CustomTags { get; set; }

    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("blurb")]
    public string Blurb { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("published_at")]
    public DateTimeOffset PublishedAt { get; set; }

    [JsonProperty("thumbnail")]
    public Banner Thumbnail { get; set; }

    [JsonProperty("banner")]
    public Banner Banner { get; set; }
}

These are my Classes below 2

public partial class Banner
{
    [JsonProperty("small")]
    public string Small { get; set; }

    [JsonProperty("medium")]
    public string Medium { get; set; }

    [JsonProperty("large")]
    public string Large { get; set; }

    [JsonProperty("original")]
    public string Original { get; set; }
}

where did I go wrong it does't even give me an error or output?

Upvotes: 1

Views: 268

Answers (2)

D-Shih
D-Shih

Reputation: 46239

Because of JSON data format result is an array, you can try this sample to make it.

Model Look like this.

public class Tag
{
    public string id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public bool subscribed { get; set; }
}

public class Thumbnail
{
    public object small { get; set; }
    public object medium { get; set; }
    public object large { get; set; }
    public object original { get; set; }
}

public class Banner
{
    public string small { get; set; }
    public string medium { get; set; }
    public string large { get; set; }
    public string original { get; set; }
}

public class Result
{
    public List<Tag> tags { get; set; }
    public List<string> custom_tags { get; set; }
    public string id { get; set; }
    public string title { get; set; }
    public string blurb { get; set; }
    public string url { get; set; }
    public DateTime published_at { get; set; }
    public Thumbnail thumbnail { get; set; }
    public Banner banner { get; set; }
}

public class RootObject
{
    public List<Result> result { get; set; }
    public DateTime first { get; set; }
    public DateTime last { get; set; }
}

var json = webClient.DownloadString(@"https:....");
 var data = JsonConvert.DeserializeObject<RootObject>(json);
 Console.WriteLine(data.result[0].title);

Upvotes: 1

Mihir Dave
Mihir Dave

Reputation: 4024

Because You used wrong Parsing models you can use Model generator

Use this code with below models

var data = JsonConvert.DeserializeObject<Root>(json);

Side note:- prefer using HttpClient instead of WebClient, Here is Good Example for that

Models:-

public partial class Root
{
    [JsonProperty("result")]
    public List<Result> Result { get; set; }

    [JsonProperty("first")]
    public DateTimeOffset First { get; set; }

    [JsonProperty("last")]
    public DateTimeOffset Last { get; set; }
}

public partial class Result
{
    [JsonProperty("tags")]
    public List<Tag> Tags { get; set; }

    [JsonProperty("custom_tags")]
    public List<string> CustomTags { get; set; }

    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("blurb")]
    public string Blurb { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("published_at")]
    public DateTimeOffset PublishedAt { get; set; }

    [JsonProperty("thumbnail")]
    public Banner Thumbnail { get; set; }

    [JsonProperty("banner")]
    public Banner Banner { get; set; }
}

public partial class Banner
{
    [JsonProperty("small")]
    public string Small { get; set; }

    [JsonProperty("medium")]
    public string Medium { get; set; }

    [JsonProperty("large")]
    public string Large { get; set; }

    [JsonProperty("original")]
    public string Original { get; set; }
}

public partial class Tag
{
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("subscribed")]
    public bool Subscribed { get; set; }
}

Upvotes: 0

Related Questions