我找到了一个示例(C# Correct way to deserialze dictionary),说明如何使用Dictionary来解析带有动态字段的json。但我似乎无法让它在我的特定json数据上工作。 我有Json数据,看起来像:
{
"status": "found",
"result": {
"ip_dns": {
"8.8.8.8": {
"fetched": 58,
"found": 58,
"items": {
"dn1.dalebulla.com": "2012-02-25",
"dns.wiseapp.net": "2014-10-16",
"dns1.reachmakers.com": "2014-10-17",
"dns2.xinhui.net": "2012-04-04",
"dnscache2.tuxserver.net": "2014-10-17",
"google-public-dns-a.google.com": "2014-10-18",
"google-public-dns-a.google.com.bge.ru": "2014-10-13",
"j6.hkidc.com": "2012-02-26",
"ns.aiky.cc": "2014-10-11",
"ns01.crimevictimsfirst.org": "2014-10-15",
"ns1.7enet.com": "2014-10-15",
"ns1.evolucionwebonline.com": "2013-07-31",
"ns1.ogloszeniownia.pl": "2012-02-25",
"ns1.start-rx.com": "2012-05-13",
"ns2.antiquar.pro": "2012-03-30",
"ns2.arboriculturajordivicent.com": "2014-10-15",
"ns2.bresunto.com": "2013-08-01",
"ns2.cafemarea.com": "2012-03-29",
"ns2.chrisdeste.com.au": "2013-08-02",
"ns2.cibercomputer.com": "2014-03-26",
"ns2.clanstars.com": "2013-08-01",
"ns2.ebella.ro": "2014-10-17",
"ns2.gameburg.net": "2014-10-16",
"ns2.i-del.ru": "2014-10-12",
"ns2.infersa.com": "2014-10-18",
"ns2.jordivicent.com": "2014-10-15",
"ns2.julioferrer.com": "2014-10-15",
"ns2.kirovnet.ru": "2014-03-22",
"ns2.mayimfelcar.com": "2014-10-16",
"ns2.moarasm.ro": "2014-03-26",
"ns2.muestrasdeverdad.com.ar": "2014-03-26",
"ns2.mv-auto.ru": "2013-08-01",
"ns2.nosolopcs.com": "2014-10-12",
"ns2.nou-model.com": "2012-02-23",
"ns2.paipernil.com": "2013-08-01",
"ns2.php-fusion.co.uk": "2014-03-21",
"ns2.sanfeliu-abogados.com": "2014-10-12",
"ns2.securittech.ro": "2014-10-18",
"ns2.stavmirsud.ru": "2014-10-14",
"ns2.tech-net.ru": "2014-10-15",
"ns2.techstation.com.ar": "2012-12-12",
"ns2.tecpandegaleana.gob.mx": "2014-10-11",
"ns2.tercermillennium.com": "2013-07-30",
"ns2.tour812.ru": "2012-05-14",
"ns2.validshop.su": "2012-12-09",
"ns2.vi-king.ru": "2014-10-11",
"ns2.wd-studios.ru": "2014-03-21",
"ns2.west-host.ro": "2014-10-15",
"ns2.westmedica.ro": "2014-03-26",
"ns2.xdiari.com": "2013-08-03",
"ns2.ylmbl.ru": "2014-10-14",
"ns3-fdns.webflip.net": "2012-12-09",
"ns3.eggplantstudios.ca": "2014-10-15",
"ns3.hosting24seven.com": "2014-10-16",
"ns3.maclean.cl": "2014-10-18",
"ns4.gnn.cz": "2012-05-15",
"ns4.norkornet.com": "2012-03-28",
"pro4.51dns.com": "2012-02-26"
}
}
}
}
}
我创建了一个类似于以下的类:
public class NameserversOnIPAddressRootobject
{
public string status { get; set; }
public NameserversOnIPAddressResult result { get; set; }
}
public class NameserversOnIPAddressResult
{
public Ip_Dns ip_dns { get; set; }
}
public class Ip_Dns
{
public Dictionary<string, CheckedServer> CheckedServers { get; set; }
}
public class CheckedServer
{
public int fetched { get; set; }
public int found { get; set; }
public Dictionary<string, string> items { get; set; }
}
public class Items
{
public Dictionary<string, string> ServerInfo { get; set; }
}
然后我使用JSON.net反序列化JSON数据:
string url = String.Format("https://url_to_get_data);
NameserversOnIPAddressRootobject returnValue = new NameserversOnIPAddressRootobject();
HttpClient client = new HttpClient();
string result = client.GetStringAsync(url).Result;
returnValue = JsonConvert.DeserializeObject<NameserversOnIPAddressRootobject>(result);
不幸的是,这并没有正确地填充类。我是否可以在不编写任何自定义转换器或手动解析结果的情况下执行此操作?
答案 0 :(得分:2)
看起来问题出在NameserversOnIPAddressResult
类定义中。其ip_dns
属性应直接为Dictionary<string, CheckedServer>
类型。 (这与items
的{{1}}属性正确为CheckedServer
类型的原因相同。)
虽然我没有手动验证这一点,但我相信受影响的代码应如下所示:
Dictionary
...您可以安全地删除public class NameserversOnIPAddressResult
{
public Dictionary<string, CheckedServer> ip_dns { get; set; }
}
类。
为了将来参考,这里的一个重要线索是,类中的所有属性都反映了JSON(例如Ip_Dns
,status
,ip_dns
),{{1除外}}