Json反序列化为自己的类

时间:2012-12-09 11:30:56

标签: asp.net-mvc-3 json.net

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

我有一个像http://api.domaintools.com/v1/domaintools.com/whois/

这样的json结构

如何将其反序列化到我自己的班级?对于我来说,实现如何构建我的类看起来有点复杂的结构?

Nam Vo

1 个答案:

答案 0 :(得分:1)

您可以使用以下类结构:

       public class Response
       {
          public string Registrant {get;set;}
          public Registration registration {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 List<string> name_servers {get;set;}

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

      }

然后你可以这样做:

    Response response = JsonSerializer.DeserializeFromString<Response>(data);//data is your data from the link you have given in the problem description

因此,现在将数据反序列化到您自己的类Response ...希望这有助于......