C# Mailchimp:将HttpWebResponse数据反序列化到自定义类

C# Mailchimp:将HttpWebResponse数据反序列化到自定义类,c#,json,httpwebrequest,mailchimp,C#,Json,Httpwebrequest,Mailchimp,我正在尝试习惯Mailchimps API,然后再进入NuGet包或外部包装,看看它有多容易使用。现在,我正在尝试获取API根目录,它应该返回我的帐户信息 我创建了我的对象来保存数据: class UserAccount { string account_id { get; set; } string account_name { get; set; } Contact contact { get; set; } bool pro_enabled { get; s

我正在尝试习惯Mailchimps API,然后再进入NuGet包或外部包装,看看它有多容易使用。现在,我正在尝试获取API根目录,它应该返回我的帐户信息

我创建了我的对象来保存数据:

class UserAccount
{
    string account_id { get; set; }
    string account_name { get; set; }
    Contact contact { get; set; }
    bool pro_enabled { get; set; }
    string last_login { get; set; }
    int total_subscribers { get; set; }
    List<Links> _links { get; set; }
}

class Contact
{
    string company { get; set; }
    string addr1 { get; set; }
    string addr2 { get; set; }
    string city { get; set; }
    string state { get; set; }
    string zip { get; set; }
    string country { get; set; }
}

class Links
{
    string rel { get; set; }
    string href { get; set; }
    string method { get; set; }
    string targetSchema { get; set; }
}

看来我提这个问题太快了。结果证明我只是个白痴,我的类数据成员不是公开的。只需简单地添加公共数据,数据就达到了应有的水平

 try {
           HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(https://us11.api.mailchimp.com/3.0/);

            request.Headers.Add("Authorization", "apikey" + apiKey);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string data = reader.ReadToEnd();
                    UserAccount account = new UserAccount();

                    account = new JavaScriptSerializer().Deserialize<UserAccount>(data);
                    var dynObject = new JavaScriptSerializer().Deserialize<dynamic>(data);
                }
            }

        }
        catch (WebException exception)
        {
            Console.WriteLine(exception.Message);
        }
{
  "account_id": "8d3a3db4d97663a9074efcc16",
  "account_name": "Freddie's Jokes",
  "contact": {
    "company": "Freddie's Jokes",
    "addr1": "675 Ponce De Leon Ave NE",
    "addr2": "Suite 5000",
    "city": "Atlanta",
    "state": "GA",
    "zip": "30308",
    "country": "US"
  },
  "last_login": "2015-09-15 14:25:37",
  "total_subscribers": 413,
  "_links": [
    {
      "rel": "self",
      "href": "https://usX.api.mailchimp.com/3.0/",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/Root.json"
    },
    {
      "rel": "lists",
      "href": "https://usX.api.mailchimp.com/3.0/lists",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/Lists/Collection.json",
      "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Lists.json"
    },
    {
      "rel": "reports",
      "href": "https://usX.api.mailchimp.com/3.0/reports",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/Reports/Collection.json",
      "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Reports.json"
    },
    {
      "rel": "conversations",
      "href": "https://usX.api.mailchimp.com/3.0/conversations",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/Conversations/Collection.json",
      "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Conversations.json"
    },
    {
      "rel": "campaigns",
      "href": "https://usX.api.mailchimp.com/3.0/campaigns",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/Campaigns/Collection.json",
      "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Campaigns.json"
    },
    {
      "rel": "automations",
      "href": "https://usX.api.mailchimp.com/3.0/automations",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/Automations/Collection.json",
      "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Automations.json"
    },
    {
      "rel": "templates",
      "href": "https://usX.api.mailchimp.com/3.0/templates",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/Templates/Collection.json",
      "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Templates.json"
    },
    {
      "rel": "file-manager",
      "href": "https://usX.api.mailchimp.com/3.0/file-manager",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/FileManager/Namespace.json"
    },
    {
      "rel": "authorized-apps",
      "href": "https://usX.api.mailchimp.com/3.0/authorized-apps",
      "method": "GET",
      "targetSchema": "https://api.mailchimp.com/schema/3.0/AuthorizedApps/Collection.json"
    }
  ]
}