0

I am currently stuck in an app that I need to build for a school project. We need to read data from an api link and show it in a list, but i cant set the variables of a class becaus it's in a tree structure and i dont know how to navigate in the response. The response is below. How do i get the String Title in a class the same is the Title from this response?

    {
        Results: [
        {
            Id: 6413,
            Feed: 1,
            Title: "'Politieteam chemicaliën werkt onder te grote druk'",
            Summary: "Het team van de politie dat dagelijks met levensgevaarlijke chemicaliën te maken krijgt bij het ontmantelen van illegale drugslabs staat onder enorme druk. Sommige werkdagen duren 24 uur.<br /> ",
            PublishDate: "2015-09-30T14:32:28",
            Image: "http://media.nu.nl/m/m1oxo80axqo8_sqr256.jpg/politieteam-chemicalien-werkt-grote-druk.jpg",
            Url: "http://www.nu.nl/binnenland/4136038/politieteam-chemicalien-werkt-grote-druk.html",
            Related: [
                "http://nu.nl/binnenland/3932577/vaten-met-chemische-xtc-stoffen-gevonden-in-garage-breugel.html",
                "http://nu.nl/binnenland/3486693/drugslaboratorium-in-huis-heerlen.html",
                "http://nu.nl/buitenland/2614100/synthetische-drugs-verdringen-cocaine.html"
            ],
            Categories: [
                    {
                        Id: 50,
                        Name: "Algemeen"
                    },
                    {
                        Id: 53,
                        Name: "Binnenland"
                    }
                    ],
            IsLiked: false
        },
        {
            Id: 6414,
            Feed: 2,
            Title: "'Nederlandse betalingsdient Adyen gewaardeerd op 2 miljard'",
            Summary: "Een Amerikaans investeringsfonds, waar onder anderen Mark Zuckerberg van Facebook en Jack Dorsey van Twitter in participeren, heeft een investering gedaan in de Nederlandse betalingsverkeerdienst Adyen. &nbsp;",
            PublishDate: "2015-09-30T14:30:29",
            Image: "http://media.nu.nl/m/m1nxkfwa72hm_sqr256.jpg/nederlandse-betalingsdient-adyen-gewaardeerd-2-miljard.jpg",
            Url: "http://www.nu.nl/internet/4136034/nederlandse-betalingsdient-adyen-gewaardeerd-2-miljard.html",
            Related: [ ],
            Categories: [
                    {
                        Id: 57,
                        Name: "Internet"
                    },
                    {
                        Id: 91,
                        Name: "Economie"
                    },
                    {
                        Id: 94,
                        Name: "Ondernemen"
                    }
                ],
            IsLiked: false
        },
    }

My failed code:

    public async Task LoadData()
    {
        try
        {
            using (var client = new HttpClient())
            {
                var json = await client.GetStringAsync("HTTPLINKTOAPI");
                Data = JsonConvert.DeserializeObject<ObservableCollection<TestModel>>(json);
            }
        }
        catch { }
    }

TestModel Class:

    public sealed class TestModel
    {
        public string Id { get; set; }
        public string Title { get; set; }
    }
DutchProgrammer
  • 107
  • 1
  • 9
  • Did you try anything so far? – tdragon Sep 30 '15 at 13:25
  • Duplicate of http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c – Phylogenesis Sep 30 '15 at 13:28
  • Not a duplicate. I dont know how to navigate trough the Results to get all the news posts – DutchProgrammer Sep 30 '15 at 13:31
  • Why do you have an empty catch? – Bauss Sep 30 '15 at 13:35
  • I am working with a code that the teacher gave me. Thats why its empty. It worked with the example api that only gave a lot of strings and id's that dont have the Results:[] around them – DutchProgrammer Sep 30 '15 at 13:40
  • I'm a touch confused about the parameters of the assignment. Is `TestModel` untouchable? Or is that your own code? If you have control over it, try decorating your model's properties with `[JsonProperty("Id")]` and `[JsonProperty("Title")]` attributes and see what happens when you deserialize. – K. Alan Bates Sep 30 '15 at 14:12
  • Everything is from the example code and needs to be changed for this kind of json structure. I can change Testmodel if i want to – DutchProgrammer Sep 30 '15 at 14:26

1 Answers1

0

First, you create a container (Results class) for all your results - Results object in your JSON. It is a collection (array) of objects (Result class) with properties like Id, Title and other collections, etc. etc...

public class ResultContent
{
    public IList<Result> Results { get; set; }
}

public class Result
{
    public int Id { get; set; }

    public string Title { get; set; }
}

var r = JsonConvert.DeserializeObject<ResultContent>(json);

Also, get rid of this empty catch, otherwise you will not know what went wrong during getting data from the API. Empty catch is always very bad practice.

tdragon
  • 3,209
  • 1
  • 16
  • 17