c# json read tree structure -
i stuck in app need build school project. need read data api link , show in list, cant set variables of class becaus it's in tree structure , dont know how navigate in response. response below. how string title in class same title 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. ", 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; } }
first, create container (results
class) results - results object in json. collection (array) of objects (result
class) properties id
, title
, 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, rid of empty catch
, otherwise not know went wrong during getting data api. empty catch
bad practice.
Comments
Post a Comment