Mailchimp:将HttpWebResponse数据反序列化为自定义类

时间:2015-12-17 14:20:10

标签: c# json httpwebrequest mailchimp

我试图习惯使用Mailchimps API,然后再跳转到NuGet包或外包装器,看看它是多么容易使用。现在,我只想尝试在API Root上进行GET,这应该返回我的帐户信息。

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

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

然后我尝试了一个简单的GET:

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

反序列化到我的自定义类会给我所有空值。反序列化到动态对象给了我所期望的一切。我唯一的猜测是它可能是类名,但我不知道它可能是什么。

这是一个&#34;示例响应&#34;来自Mailchimps API页面:

{
  "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"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

我似乎过快地提出了这个问题。事实证明,我只是一个白痴,我的班级数据成员不公开。通过简单地添加公共数据,数据就应该存在。