Hi,
I'm struggling to figure out how I need to create a JSON string to send to an external API (Campaign Monitor). Unfortunately this being CRM Online I can't use there wrappers which would have made things a lot easier.
I have two main queries (assuming I'm approaching this correctly). 1 I need to format my DataContract correctly so I can build the child elements. I currently have this but don't think it's quite right:
[DataContract(Namespace = "")]
public class smartEmail
{
[DataMember]
public string To { get; set; }
[DataMember]
public IEnumerable<KeyValuePair<string, string>> Data { get; set; }
[Serializable]
public struct KeyValuePair<K, V>
{
public K Key { get; set; }
public V Value { get; set; }
}
}
It needs to look something like:
{
"To": [
"Joe Smith <joesmith@example.com>"
],
"Data": {
"firstname": "Joe",
"lastname": "Smith"
}
}
And secondly my code to generate the object, particularly the data element. Here is what I have:
//create data
Dictionary<string, object> data = new Dictionary<string, object> { };
data.Add("firstname", "Joe");
data.Add("lastname", "Smith");
//"create email
smartEmail CMEmail = new smartEmail();
CMEmail.To = "Joe Smith<joesmith@example.com>";
//Build JS
string wsData = "";
using (var ms = new MemoryStream())
{
var js = new DataContractJsonSerializer(typeof(smartEmail));
js.WriteObject(ms, CMEmail);
ms.Position = 0;
var sr = new StreamReader(ms);
wsData = sr.ReadToEnd();
}
// deal with wsData string...
*This post is locked for comments
I have the same question (0)