Question

In Visual Studio/ C# I am retreiving the JSON from this url: http://hotell.difi.no/api/json/brreg/enhetsregisteret?query=knart

When trying to display any of the entries in a MessageBox, nothing is displayed.

Can anyone suggest what I 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) where T : new()
    {
        using (var w = new WebClient())
        {
            var json_data = string.Empty;
            // attempt to download JSON data as a string
            try
            {
                json_data = w.DownloadString(url);
            }
            catch (Exception) { }
            // if string with JSON data is not empty, deserialize it to class and return its 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);
    }
}

}

Was it helpful?

Solution

It doesn't look like your Entry class matches the JSON being returned.

The JSON contains an array of Entry called "entries" and a few other properties, not a bare Entry. Try

_download_serialized_json_data<RootObject>(url);

OTHER TIPS

This is what I needed to do.

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;
        RootObject brreg_entries = _download_serialized_json_data<RootObject>(url);
        var orgnr = brreg_entries.entries[0].orgnr;
        MessageBox.Show(url + "\r\n" + "Result: " + orgnr);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top