0

我正在使用 asp.net mvc4 和 json.net

我有一个像http://api.domaintools.com/v1/domaintools.com/whois/这样的 json 结构

我怎样才能将它反序列化到我自己的类中?这对我来说看起来有点复杂的结构来实现如何构建我的类?

到目前为止我已经尝试过:

 var json = "{\"response\":{\"registrant\":\"DomainTools, LLC\",\"registration\":{\"created\":\"1998-08-02\",\"expires\":\"2014-08-01\",\"updated\":\"2010-08-31\",\"registrar\":\"CHEAP-REGISTRAR.COM\",\"statuses\":[\"ok\"]},\"name_servers\":[\"NS1.P09.DYNECT.NET\",\"NS2.P09.DYNECT.NET\",\"NS3.P09.DYNECT.NET\",\"NS4.P09.DYNECT.NET\"],\"whois\":{\"date\":\"2013-05-12\",\"record\":\"Domain name: domaintools.com\\n\\nRegistrant Contact:\\n   DomainTools, LLC\\n   Domain Administrator (memberservices@domaintools.com)\\n   +1.2068389035\\n   Fax: +1.2068389056\\n   2211 5th Avenue\\n   Suite 201\\n   Seattle, WA 98121\\n   US\\n\\nAdministrative Contact:\\n   DomainTools, LLC\\n   Domain Administrator (memberservices@domaintools.com)\\n   +1.2068389035\\n   Fax: +1.2068389056\\n   2211 5th Avenue\\n   Suite 201\\n   Seattle, WA 98121\\n   US\\n\\nTechnical Contact:\\n   DomainTools, LLC\\n   Domain Administrator (memberservices@domaintools.com)\\n   +1.2068389035\\n   Fax: +1.2068389056\\n   2211 5th Avenue\\n   Suite 201\\n   Seattle, WA 98121\\n   US\\n\\n   Status: Active\\n   Creation Date: 13-Jul-2002\\n   Expiration Date: 13-Jul-2016\\n   Name Server: NS1.P09.DYNECT.NET\\n   Name Server: NS2.P09.DYNECT.NET\\n   Name Server: NS3.P09.DYNECT.NET\\n   Name Server: NS4.P09.DYNECT.NET\\n\"}}}";

public class Response
    {
        public string registrant { get; set; }
        public Registration registration { get; set; }
        public List<string> name_servers { get; set; }
        public WhoIs whois { get; set; }
    }
    public class Registration
    {
        public DateTime created { get; set; }
        public DateTime expires { get; set; }
        public DateTime updated { get; set; }
        public string registrar { get; set; }
        public List<string> statuses { get; set; }


    }
    public class WhoIs
    {
        public DateTime date { get; set; }
        public string record { get; set; }

    }
 var response = JsonConvert.DeserializeObject<Response>(json);  >> null
4

1 回答 1

1

从http://json2csharp.com/站点开始找到正确的结构总是好的,对于您的示例,它提供:

public class Registration
{
    public string created { get; set; }
    public string expires { get; set; }
    public string updated { get; set; }
    public string registrar { get; set; }
    public List<string> statuses { get; set; }
}

public class Whois
{
    public string date { get; set; }
    public string record { get; set; }
}

public class Response
{
    public string registrant { get; set; }
    public Registration registration { get; set; }
    public List<string> name_servers { get; set; }
    public Whois whois { get; set; }
}

public class RootObject
{
    public Response response { get; set; }
}

您将需要RootObject用作要反序列化的类:

var response = JsonConvert.DeserializeObject<RootObject>(json);
于 2013-05-14T04:36:34.510 回答