6

我正在尝试创建一个 JSON 请求以发送到电子邮件服务 GetResponse 以将联系人添加到邮件活动。

我想要实现的格式是add_contact

[
    "API_KEY",
    {
        "campaign"  : "CAMPAIGN_ID",
        "action"    : "action_value",
        "name"      : "name_value",
        "email"     : "email_value",
        "cycle_day" : cycle_day_value,
        "ip"        : "ip_value",
        "customs"   : [
            {
                "name"      : "name_1_value",
                "content"   : "content_1_value"
            },
            {
                "name"      : "name_2_value",
                "content"   : "content_2_value"
            }
        ]
    }
]

如何在 C# 中创建 JSON 字符串之后,我构建了这个设置

private class AddContactRequest
{
    public string campaign { get; set; }
    public string action { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public int cycle_day { get; set; }
    public string ip { get; set; }
}

像这样填满

AddContactRequest add = new AddContactRequest();
add.campaign = campaignID;
add.action = action
add.name = contact_name;
add.email = email;
add.cycle_day = cycle_day;
add.ip = ip_value;

string json = new JavaScriptSerializer().Serialize(add);

这里json符合预期

{"campaign":"my_test_campaign","action":"standard","name":"Test Name","email":"test@example.com","cycle_day":0,"ip":"10.1.0.5"}

我不知道如何正确地将API_KEY字符串添加到它的前面。

如何更改它以将其添加API_KEY到它的前面而不附加属性名称,理想情况下使用JavaScriptSerializer()

4

2 回答 2

4

您需要做的是创建一个类型集合并作为第一个条目object传递,您的对象作为第二个条目传递。"API_KEY"add

AddContactRequest add = new AddContactRequest();
add.campaign = campaignID;
add.action = action
add.name = contact_name;
add.email = email;
add.cycle_day = cycle_day;
add.ip = ip_value;

List<object> SerializedThingy = new List<object>
{
   "API_KEY",
   add
};

string json = new JavaScriptSerializer().Serialize(SerializedThingy);
于 2013-09-07T03:44:33.713 回答
1

从文档来看,他们似乎正在以专有方式包装 JSON 对象。JSON 需要以“{”开头,而不是“[”。我会尝试这样的事情:

string json = new JavaScriptSerializer().Serialize(SerializedThingy);
string req = "[ \"API_KEY\", " + json + " ]";
于 2013-09-07T03:53:23.377 回答