1

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?

1
  • 2
    The data for Result is under the result property, so you are missing a root class: public class SomeName{ public Result Result { get; set; } } Commented Jul 13, 2018 at 19:40

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

You are a GOD, Thanks alot...I'm on Mac...what can I use as an alternative to Web Essentials? for MacOS
Hi @Komicon I would suggest you create another thread for this question, because of it is another question.
I did but no Helped it's been 4 days now
0

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

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.