对于C#我很新,我正在寻找对以下内容的帮助。将JSON从GET请求转换为C#Winforms应用程序中的文本框
我已经编写了一些利用公司房屋API获取与公司有关的信息的代码(如下)。信息以JSON格式返回。
我需要做些什么来获取某些信息,并将其放入表单中已有的文本框中。我编写的代码能够在消息框中返回,但我无法解决如何将其填充到文本框中。
private void getFromCompaniesHouse(object sender, EventArgs e)
{
try
{
//Compile request url
string url = "https://api.companieshouse.gov.uk/company/" + txtCompanyNumber.Text;
// Encode API key to ASCII
string api_key = "yfhOb66cRn7ZL1VgdFjVur5cs8u6j__bcNnKj9Qs:";
string encodedKey = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(api_key));
// Make get request using url and encoded API key
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Headers.Add("Authorization", "Basic " + encodedKey);
request.ContentType = "application/json; charset=utf-8";
var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
MessageBox.Show(streamReader.ReadToEnd());
}
}
catch
{
MessageBox.Show("Cannot Make Request", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
的JSON返回这个样子的:
{
"accounts":{
"accounting_reference_date":{
"month":"12",
"day":"31"
},
"last_accounts":{
"type":"group",
"made_up_to":"2015-12-31"
},
"next_made_up_to":"2016-12-31",
"next_due":"2017-09-30",
"overdue":false
},
"company_number":"08867781",
"annual_return":{
"last_made_up_to":"2016-06-20",
"overdue":false
},
"jurisdiction":"england-wales",
"has_been_liquidated":false,
"date_of_creation":"2014-01-29",
"undeliverable_registered_office_address":false,
"company_name":"VIRGIN ATLANTIC LIMITED",
"registered_office_address":{
"address_line_2":"Fleming Way",
"locality":"Crawley",
"country":"United Kingdom",
"region":"West Sussex",
"address_line_1":"Company Secretariat - The VHQ",
"postal_code":"RH10 9DF"
},
"type":"ltd",
"last_full_members_list_date":"2016-06-20",
"sic_codes":[
"70100"
],
"has_insolvency_history":false,
"etag":"cbab10bb8b9dc1db442cb585a63ae208c1265100",
"company_status":"active",
"has_charges":false,
"previous_company_names":[
{
"name":"VIRGIN ATLANTIC (HOLDINGS) LIMITED",
"effective_from":"2014-01-29",
"ceased_on":"2014-05-30"
}
],
"confirmation_statement":{
"next_made_up_to":"2017-06-20",
"overdue":false,
"next_due":"2017-07-04"
},
"links":{
"self":"/company/08867781",
"filing_history":"/company/08867781/filing-history",
"officers":"/company/08867781/officers"
},
"registered_office_is_in_dispute":false,
"can_file":true
}
**更新**
我有我的代码更新到以下几点:
private void getFromCompaniesHouse(object sender, EventArgs e)
{
try
{
//Compile request url
string url = "https://api.companieshouse.gov.uk/company/" + txtCompanyNumber.Text;
// Encode API key to ASCII
string api_key = "yfhOb66cRn7ZL1VgdFjVur5cs8u6j__bcNnKj9Qs:";
string encodedKey = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(api_key));
// Make get request using url and encoded API key
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Headers.Add("Authorization", "Basic " + encodedKey);
request.ContentType = "application/json; charset=utf-8";
var response = (HttpWebResponse)request.GetResponse();
var rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd();
var json = JObject.Parse(rawJson); //Turns your raw string into a key value lookup
string company_name = json["company_name"].ToObject<string>();
txtBusinessName.Text = company_name;
}
catch
{
MessageBox.Show("Cannot Make Request", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我可以拉东西从RootObject
类,但不能访问其他任何东西,例如在RegisteredOfficeAddress
课。有任何想法吗?
很难帮助不知道JSON的样子,这里的“某些信息”是和/或如果您知道如何反序列化它 – Plutonix
你有没有为JSON定义的结构返回? – Eric
这里有几个步骤。一般来说,您想使用库来将JSON转换为C#类(google C#反序列化json等),然后您可以将信息从那里拉出并粘贴到文本框上,就像您通常设置文本一样。 – mmcrae