反序列化在不同 MailChimp 列表中具有不同名称的合并字段和兴趣

Deserialize merge-fields and interests that have different names in different MailChimp lists

反序列化以下 json 响应时,我设法获取订阅者 ID、电子邮件和状态,这很容易,因为这些名称始终相同并且不依赖于列表中的特定设置.但是我如何反序列化在不同列表中具有不同名称的合并字段和兴趣?

{ "members": [ {
    "id": "f777bbffab8d1ceca8b757df63c47cb8",
    "email_address": "urist.mcvankab+1@freddiesjokes.co",
    "unique_email_id": "882e9bec19",
    "status": "subscribed",
    "merge_fields":{
        "FNAME": "",
        "LNAME": "" },
    "interests":{
        "9143cf3bd1": true,
        "3a2a927344": false,
        "f9c8f5f0ff": false,
        "f231b09abc": true,
        "bd6e66465f": false },
//And so on...

如何更改我的 classes 以反序列化响应?

我的会员Class

public class Member
{
    public string id { get; set; }
    public string email_address { get; set; }
    public string status { get; set; }
    //public List<MergeField> mergeFields { get; set; }
    //public List<Interests> interests { get; set; }
}

MergeField 指的是我的旧 MergeField class,其中字段是硬编码的

public class MergeField
{
    public string FNAME { get; set; }
    public string LNAME { get; set; }
    public string ADDRESS { get; set; }
    public string MERGE4 { get; set; }
}

但我想使用相同的两个 Mergefieldclasses,我在请求列表的合并字段时用于反序列化 json 响应。

public class MergeFields
{
    public List<Mergefield> merge_fields { get; set; }
    public string list_id { get; set; }
    public int total_items { get; set; }
}

public class Mergefield
{
    public string merge_id { get; set; }
    public string tag { get; set; }
    public string name { get; set; }
    public string list_id { get; set; }
}

我找到了这个答案 ,但我不知道如何在我的场景中使用它。

您可以为合并字段使用 Dictionary<string, object> 类型,为兴趣使用 Dictionary<string, bool> 类型。您的 Member class 应该是这样的

public class Member
{
    public string id { get; set; }
    public string email_address { get; set; }
    public string status { get; set; }
    public Dictionary<string, object> merge_fields { get; set; }
    public Dictionary<string, bool> interests { get; set; }
}

假设您使用 MemberResponse class 进行如下响应

public class MemberResponse
{
    public List<Member> members { get; set; }
    public string list_id { get; set; }
    public int total_items { get; set; }
}

以下是获取每个合并字段和兴趣的值的方法

MemberResponse memberResponse = .... // your code that calls the API

// enumerate members
foreach (var member in memberResponse.members)
{
    // enumerate merge fields
    foreach (var key in member.merge_fields.Keys)
    {
        // when key = "FNAME", value would be the first name of the member
        // when key = "LNAME", value would be the last name of the member
        string value = member.merge_fields[key].ToString();
    }

    // enumerate interests
    foreach (var key in member.interests.Keys)
    {
        // when key = "9143cf3bd1", value would be true
        // when key = "3a2a927344", value would be false
        // ... and so on
        bool value = member.interests[key];
    }

}

遗憾的是,无法使用 /lists/{list_id}/members 端点获取合并字段的 namemerge_id,因为响应正文仅包含合并字段的 tag字段,即 FNAMELNAME 等。您需要单独调用 /lists/{list_id}/merge-fields 端点并比较每个合并字段的 tag 以获得相关 namemerge_id.