【发布时间】:2016-03-24 01:19:13
【问题描述】:
在跳入 NuGet 包或外部包装器之前,我正在尝试习惯 Mailchimps API,以了解它的易用性。现在,我只是尝试在 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);
}
反序列化到我的自定义类会给我所有的空值。反序列化为动态对象给了我我期望的一切。我唯一的猜测是它可能是类名,但我不知道它可能是什么。
这是来自 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"
}
]
}
【问题讨论】:
标签: c# json httpwebrequest mailchimp