在Visual Studio / C#中,我正从此网址中检索JSON: http://hotell.difi.no/api/json/brreg/enhetsregisteret?query=knart
尝试显示MessageBox中的任何条目时,不显示任何内容。
有人可以建议我需要什么吗?
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);
}
}
}
答案 0 :(得分:0)
看起来您的Entry类与返回的JSON不匹配。
JSON包含一个名为“entries”的条目数组和一些其他属性,而不是一个裸条目。尝试
_download_serialized_json_data<RootObject>(url);
答案 1 :(得分:0)
这就是我需要做的。
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);
}