解析具有动态属性的JSON
我找到了一个示例( C#正确解序列字典的方法),了解如何使用字典来解析具有动态字段的json.但是我似乎无法使它适用于我的特定json数据. 我的Json数据如下所示:
I found an example (C# Correct way to deserialze dictionary) of how to use a Dictionary to parse json with dynamic fields. But I can't seem to get it to work on my specifc json data. I have Json data that looks like:
{
"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"
}
}
}
}
}
我创建了一个看起来像这样的类:
I created a classes that looks like:
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数据:
Then I use JSON.net to deserialize the JSON data:
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);
不幸的是,这不能正确地填充类.无需编写任何类型的自定义转换器或手动解析结果就可以做到这一点吗?
Unfortunately this does not populate the classes correctly. Should I be able to do this without writing any sort of custom converter or manually parse the result?
问题似乎出在NameserversOnIPAddressResult
类定义中.其ip_dns
属性应直接为Dictionary<string, CheckedServer>
类型. (出于同样的原因,CheckedServer
的items
属性正确地是Dictionary
类型.)
It looks like the issue is in the NameserversOnIPAddressResult
class definition. Its ip_dns
property should be directly of type Dictionary<string, CheckedServer>
. (This is for the same reason that the items
property of CheckedServer
is correctly a Dictionary
type.)
尽管我还没有手动验证这一点,但我相信受影响的代码应如下所示:
Although I haven't manually verified this, I believe the affected code should look like:
public class NameserversOnIPAddressResult
{
public Dictionary<string, CheckedServer> ip_dns { get; set; }
}
...,您就可以安全地删除Ip_Dns
类.
...and you can safely remove the Ip_Dns
class.
为了将来参考,这里的一个重要线索是,类中的所有属性都镜像JSON(例如status
,ip_dns
,fetched
),但CheckedServers
除外.
For future reference, an important clue here is that all of the properties in your classes mirror the JSON (e.g. status
, ip_dns
, fetched
), with exception of CheckedServers
.