开发者

Php 2d array as C# 2d array/struct

开发者 https://www.devze.com 2022-12-27 18:10 出处:网络
I\'m using MailChimp\'s API to subscribe email to a list. Function listsubscribe() is used for email subscription:

I'm using MailChimp's API to subscribe email to a list. Function listsubscribe() is used for email subscription:

public static  listSubscribe(string apikey, string id, string email_address, array merge_vars, string email_type, boolean double_optin, boolean update_existing, boolean replace_interests, boolean send_welcome)

I downloaded MailChimp's official .NET wrapper 开发者_JAVA技巧for their API

When looking in Visual Studio, this is one of overloaded functions:

listSubscribe(string apikey, string id, string email_address, MCMergeVar[] merges)

When I click on definition of MCMergeVar[], this comes out:

[XmlRpcMissingMapping(MappingAction.Ignore)]
    public struct MCMergeVar
    {
        public string name;
        public bool req;
        [XmlRpcMissingMapping(MappingAction.Error)]
        public string tag;
        public string val;
    }

In a php example on MailChimp's website, this is how merges variable is declared:

$merge_vars = array('FNAME'=>'Test', 'LNAME'=>'Account', 'INTERESTS'=>'');

How to write this array correctly for my C# wrapper? I tried something like this:

MCMergeVar[] subMergeVars = new MCMergeVar[1];
subMergeVars["FNAME"] = "Test User";

But it requires an int in place where "FNAME" is now placed, so this doesn't work...

Thanks in advance, Ile

EDIT 1: I tried FoxFire's solution but no data from subMergeVars is passed to MailChimp server, only email is passed:

// Subscribe email to list
            string subID = "26973e52cc";
            string subEmail = "mymail@some.com.hr";
            MCMergeVar[] subMergeVars = new MCMergeVar[5];
            subMergeVars[0].name = "FNAME";
            subMergeVars[0].val = "FNDynamic";
            subMergeVars[1].name = "LNAME";
            subMergeVars[1].val = "LNDynamic";

            mailChimp.api.listSubscribe(subID, subEmail, subMergeVars, "html");


Most likely:

MCMergeVar[] subMergeVars = new MCMergeVar[1]; 
subMergeVars[0].name = "FNAME"; 
subMergeVars[0].val = "Test User"; 


Try:

var mergeVars = new List<MCMergeVar>();

mergeVars.Add(new MCMergeVar() { tag = "FNAME", val = "Test User First Name" });
mergeVars.Add(new MCMergeVar() { tag = "LNAME", val = "Test User Last Name" });

Then use:

mergeVars.ToArray()
0

精彩评论

暂无评论...
验证码 换一张
取 消