c# - Retrieving JSON from a web site - What am I missing? -
in visual studio/ c# retreiving json url: http://hotell.difi.no/api/json/brreg/enhetsregisteret?query=knart
when trying display of entries in messagebox, nothing displayed.
can suggest need?
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.data.sql; using system.data.sqlclient; using system.net; // webclient/ json via http using system.io; using system.web.script.serialization; // json.net using newtonsoft.json.linq; using newtonsoft.json; //json.net namespace json_test { public partial class form1 : form { public form1() { initializecomponent(); } public class entry { public string nkode2 { get; set; } public string nkode1 { get; set; } public string tlf_mobil { get; set; } public string organisasjonsform { get; set; } public string ppoststed { get; set; } public string tvangsavvikling { get; set; } public string forretningsadr { get; set; } public string regifriv { get; set; } public string orgnr { get; set; } public string forradrland { get; set; } public string stiftelsesdato { get; set; } public string forradrkommnr { get; set; } public string konkurs { get; set; } public string regdato { get; set; } public string avvikling { get; set; } public string regifr { get; set; } public string forradrpoststed { get; set; } public string ppostland { get; set; } public string forradrkommnavn { get; set; } public string forradrpostnr { get; set; } public string navn { get; set; } public string regnskap { get; set; } public string url { get; set; } public string sektorkode { get; set; } public string regimva { get; set; } public string ppostnr { get; set; } public string postadresse { get; set; } public string nkode3 { get; set; } public string regiaa { get; set; } public string tlf { get; set; } } public class rootobject { public list<entry> entries { get; set; } public int page { get; set; } public int pages { get; set; } public int posts { get; set; } } private static t _download_serialized_json_data<t>(string url) t : new() { using (var w = new webclient()) { var json_data = string.empty; // attempt download json data string try { json_data = w.downloadstring(url); } catch (exception) { } // if string json data not empty, deserialize class , return instance return !string.isnullorempty(json_data) ? jsonconvert.deserializeobject<t>(json_data) : new t(); } } private void button1_click_1(object sender, eventargs e) { string querystring = textbox1.text; var url = "http://hotell.difi.no/api/json/brreg/enhetsregisteret?query=" + querystring; var brreg = _download_serialized_json_data<entry>(url); string orgnr = brreg.orgnr; messagebox.show(url + "\r\n" + "result: " + orgnr); } }
}
it doesn't entry class matches json being returned.
the json contains array of entry called "entries" , few other properties, not bare entry. try
_download_serialized_json_data<rootobject>(url);
Comments
Post a Comment