正在分析具有动态属性的JSON



我找到了一个如何使用dictionary解析带有动态字段的json的示例(C#取消字典的正确方法)。但我似乎无法让它在我的特定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);

不幸的是,这并不能正确地填充类。我应该在不编写任何自定义转换器或手动解析结果的情况下完成这项工作吗?

问题似乎在NameserversOnIPAddressResult类定义中。其ip_dns属性应直接为Dictionary<string, CheckedServer>类型。(这与CheckedServeritems属性正确地为Dictionary类型的原因相同。)

虽然我还没有手动验证,但我相信受影响的代码应该是这样的:

public class NameserversOnIPAddressResult
{
    public Dictionary<string, CheckedServer> ip_dns { get; set; }
}

并且可以安全地移除Ip_Dns类。

为了将来参考,这里的一条重要线索是,类中的所有属性都镜像JSON(例如statusip_dnsfetched),CheckedServers除外。

相关内容

  • 没有找到相关文章

最新更新