有人可以帮我反序列化这个JSON文件吗?
{
"result": "success",
"records": {
"504498959": {
"nif": 504498959,
"seo_url": "triworld-equipamentos-informaticos-e-de-telecomunicacoes-unipessoal-lda",
"title": "Triworld - Equipamentos Inform\u00e1ticos e de Telecomunica\u00e7\u00f5es, Unipessoal Lda",
"address": "Rua Doutor Francisco Noronha, N 31",
"pc4": "4700",
"pc3": "355",
"city": "Braga",
"activity": "O com\u00e9rcio, importa\u00e7\u00f5es, exporta\u00e7\u00f5es e representa\u00e7\u00f5es de material e equipamento inform\u00e1tico, de telecomunica\u00e7\u00f5es; montagem, instala\u00e7\u00e3o, assist\u00eancia t\u00e9cnica, desenvolvimento de programas inform\u00e1ticos e outros similares.",
"status": "active",
"cae": "47781",
"contacts": {
"email": null,
"phone": "253261844",
"website": null,
"fax": "253613646"
},
"structure": {
"nature": "UNI",
"capital": "0.00",
"capital_currency": "EUR"
},
"geo": {
"region": "Braga",
"county": "Braga",
"parish": "S\u00e3o Vicente"
},
"place": {
"address": "Rua Doutor Francisco Noronha, N 31",
"pc4": "4700",
"pc3": "355",
"city": "Braga"
},
"racius": "http:\/\/www.racius.com\/triworld-equipamentos-informaticos-e-de-telecomunicacoes-unipessoal-lda\/",
"alias": "Triworld - Equip. Informaticos e de Telecomunica\u00e7\u00f5es,unipes., Lda",
"portugalio": "http:\/\/www.portugalio.com\/triworld-equip-informaticos-e-de-telecomunicacoes-unipes\/"
}
},
"nif_validation": true,
"is_nif": true,
"credits": {
"used": "Triworld - Equipamentos Informáticos e de Telecomunicações, Unipessoal Lda",
"left": {
"month": 993,
"day": 99,
"hour": 9,
"minute": 0,
"paid": 0
}
}
}
我正在使用此代码
using Newtonsoft.Json;
public class Rootobject
{
public string result { get; set; }
public Records records { get; set; }
public bool nif_validation { get; set; }
public bool is_nif { get; set; }
public Credits credits { get; set; }
}
public class Records
{
public _504498959 _504498959 { get; set; }
}
public class _504498959
{
public int nif { get; set; }
public string seo_url { get; set; }
public string title { get; set; }
public string address { get; set; }
public string pc4 { get; set; }
public string pc3 { get; set; }
public string city { get; set; }
public string activity { get; set; }
public string status { get; set; }
public string cae { get; set; }
public Contacts contacts { get; set; }
public Structure structure { get; set; }
public Geo geo { get; set; }
public Place place { get; set; }
public string racius { get; set; }
public string alias { get; set; }
public string portugalio { get; set; }
}
public class Contacts
{
public object email { get; set; }
public string phone { get; set; }
public object website { get; set; }
public string fax { get; set; }
}
public class Structure
{
public string nature { get; set; }
public string capital { get; set; }
public string capital_currency { get; set; }
}
public class Geo
{
public string region { get; set; }
public string county { get; set; }
public string parish { get; set; }
}
public class Place
{
public string address { get; set; }
public string pc4 { get; set; }
public string pc3 { get; set; }
public string city { get; set; }
}
public class Credits
{
public string used { get; set; }
public Left left { get; set; }
}
public class Left
{
public int month { get; set; }
public int day { get; set; }
public int hour { get; set; }
public int minute { get; set; }
public int paid { get; set; }
}
protected void VerificaNif(object sender, EventArgs e)
{
Rootobject jsonfileroot = JsonConvert.DeserializeObject<Rootobject>(get_web_content("http://www.triworld.pt/4FPIL83K.json"));
// need to use "records": { "504498959": { "title": "Triworld - Equipamentos Inform\u00e1ticos e de Telecomunica\u00e7\u00f5es, Unipessoal Lda", on a textbox named Nome.Text
if (jsonfileroot.result == "success")
{
Nome.Text = jsonfileroot.records._504498959.title;
}
else
{
erro.Text = " Nif is Invalid";
}
}
但它给了我一个NullReferenceException
,它无法达到标题。
答案 0 :(得分:2)
更改类的声明,如下所示。完成后,以下代码正确反序列化。这是我的控制台应用程序的输出。
class Program
{
static void Main(string[] args)
{
string theJson = "{ \"result\": \"success\", \"records\": { \"504498959\": { \"nif\": 504498959, \"seo_url\": \"triworld-equipamentos-informaticos-e-de-telecomunicacoes-unipessoal-lda\", \"title\": \"Triworld - Equipamentos Inform\u00e1ticos e de Telecomunica\u00e7\u00f5es, Unipessoal Lda\", \"address\": \"Rua Doutor Francisco Noronha, N 31\", \"pc4\": \"4700\", \"pc3\": \"355\", \"city\": \"Braga\", \"activity\": \"O com\u00e9rcio, importa\u00e7\u00f5es, exporta\u00e7\u00f5es e representa\u00e7\u00f5es de material e equipamento inform\u00e1tico, de telecomunica\u00e7\u00f5es; montagem, instala\u00e7\u00e3o, assist\u00eancia t\u00e9cnica, desenvolvimento de programas inform\u00e1ticos e outros similares.\", \"status\": \"active\", \"cae\": \"47781\", \"contacts\": { \"email\": null, \"phone\": \"253261844\", \"website\": null, \"fax\": \"253613646\" }, \"structure\": { \"nature\": \"UNI\", \"capital\": \"0.00\", \"capital_currency\": \"EUR\" }, \"geo\": { \"region\": \"Braga\", \"county\": \"Braga\", \"parish\": \"S\u00e3o Vicente\" }, \"place\": { \"address\": \"Rua Doutor Francisco Noronha, N 31\", \"pc4\": \"4700\", \"pc3\": \"355\", \"city\": \"Braga\" }, \"racius\": \"http://www.racius.com/triworld-equipamentos-informaticos-e-de-telecomunicacoes-unipessoal-lda/\", \"alias\": \"Triworld - Equip. Informaticos e de Telecomunica\u00e7\u00f5es,unipes., Lda\", \"portugalio\": \"http://www.portugalio.com/triworld-equip-informaticos-e-de-telecomunicacoes-unipes/\" } }, \"nif_validation\": true, \"is_nif\": true, \"credits\": { \"used\": \"Triworld - Equipamentos Informáticos e de Telecomunicações, Unipessoal Lda\", \"left\": { \"month\": 993, \"day\": 99, \"hour\": 9, \"minute\": 0, \"paid\": 0 } } }";
Rootobject theRecords = JsonConvert.DeserializeObject<Rootobject>(theJson);
if (theRecords != null)
{
Console.WriteLine("Deserialization succeeded :)");
if (theRecords.result == "success")
{
Console.WriteLine(" # of Records: {0}", theRecords.records.Count);
int recordCount = 1;
foreach (var record in theRecords.records)
{
Console.WriteLine(" Record #{0}", recordCount);
Console.WriteLine(" Key: {0}", record.Key);
Console.WriteLine(" nif: {0}", record.Value.nif);
Console.WriteLine(" seo_url: {0}", record.Value.seo_url);
Console.WriteLine(" title: {0}", record.Value.title);
Console.WriteLine(" address: {0}", record.Value.address);
Console.WriteLine(" city: {0}", record.Value.city);
Console.WriteLine(" alias: {0}", record.Value.alias);
Console.WriteLine(" portugalio : {0}", record.Value.portugalio);
recordCount++;
}
}
}
else
{
Console.WriteLine("Deserialization failed :(");
}
Console.ReadLine();
}
}
以下是修改类声明的方法。
public class Rootobject
{
public string result { get; set; }
public Dictionary<int, Location> records { get; set; }
public bool nif_validation { get; set; }
public bool is_nif { get; set; }
public Credits credits { get; set; }
}
public class Location
{
public int nif { get; set; }
public string seo_url { get; set; }
public string title { get; set; }
public string address { get; set; }
public string pc4 { get; set; }
public string pc3 { get; set; }
public string city { get; set; }
public string activity { get; set; }
public string status { get; set; }
public string cae { get; set; }
public Contacts contacts { get; set; }
public Structure structure { get; set; }
public Geo geo { get; set; }
public Place place { get; set; }
public string racius { get; set; }
public string alias { get; set; }
public string portugalio { get; set; }
}
public class Contacts
{
public object email { get; set; }
public string phone { get; set; }
public object website { get; set; }
public string fax { get; set; }
}
public class Structure
{
public string nature { get; set; }
public string capital { get; set; }
public string capital_currency { get; set; }
}
public class Geo
{
public string region { get; set; }
public string county { get; set; }
public string parish { get; set; }
}
public class Place
{
public string address { get; set; }
public string pc4 { get; set; }
public string pc3 { get; set; }
public string city { get; set; }
}
public class Credits
{
public string used { get; set; }
public Left left { get; set; }
}
public class Left
{
public int month { get; set; }
public int day { get; set; }
public int hour { get; set; }
public int minute { get; set; }
public int paid { get; set; }
}
RootObject
有Dictionary<int, Location> records
,其中int
值为关键字,对象包含在Location
对象中。
如果int
值在您的最终json中可能重复,则将其更改为List<NameValuePair<string,Location>>
records
实体的数字键将是Dictionary
内RootObject
对象的键。 value
将是名为Location
的类(我给它起了名字,也许更适合。你会最了解。