0

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; }
}

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?

1 Answer 1

2

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; }
}

...and you can safely remove the Ip_Dns class.

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.