Aximili
Aximili

Reputation: 29484

JSON multi-dimensional array/Dictionary in C# for MailChimp

I have a working code that sends subscriber's email to MailChimp in C#.

I only need to add FirstName to it. From the documentation, merge_vars seems to be a multi-dimensional JSON array. How do you do it in C#?

This is the code

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
  Dictionary<string,string> data = new Dictionary<string,string>();
  data.Add("apikey", ApiKey);
  data.Add("id", ListId);
  data.Add("email_address", email);
  //data.Add("merge_vars[FNAME]", firstname); // <-- This is what I tried adding but doesn't work

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

  streamWriter.Write(json);
  streamWriter.Flush();
  streamWriter.Close();

  var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  {
    var response = streamReader.ReadToEnd();
    //...
  }
}

Upvotes: 1

Views: 693

Answers (1)

SimonC
SimonC

Reputation: 6718

Have you tried:

data.Add("merge_vars", new Dictionary<string, string> { {"FNAME", firstname} });

Upvotes: 2

Related Questions