使用 Java API TransmissionWithRecipientArray 对象,如何将元素设置为键值数组 (Sparkpost)
Using Java API TransmissionWithRecipientArray object, how can I set an element like a key value array ( Sparkpost )
我正在根据模板使用 Java API TransmissionWithRecipientArray 对象发送电子邮件。我在替换数据方面遇到了一些问题。我已经在模板编辑器中测试了这些数据,但我不知道如何使用 TransmissionWithRecipientArray 引入该替换数据。
这是一个示例:
(...), "offers": [
{
"description": "dddddddddddddddddd.",
"discount": "ddddddd",
"image": "ddddddddddddddddddddd",
"image_announcer": "dddddddddddddddddddddddddddd",
"alt_title": "dddddddddddddddddddddd",
"tracking": "dhsdjkhsdjksdh",
"name": "sdhsdohdsiosd",
"id": "8480515",
"announcer_paid": "0",
"announcer_image": "test",
"announcer_alt_title": "wdiohdiowdhiowd"
},
{
"description": "dddddddddddddddddd.",
"discount": "ddddddd",
"image": "ddddddddddddddddddddd",
"image_announcer": "dddddddddddddddddddddddddddd",
"alt_title": "dddddddddddddddddddddd",
"tracking": "dhsdjkhsdjksdh",
"name": "sdhsdohdsiosd",
"id": "8480515",
"announcer_paid": "0",
"announcer_image": "test",
"announcer_alt_title": "wdiohdiowdhiowd"
}, (...)
换句话说,问题是:我们应该在 setSubstitutionData() 方法中引入什么来获取此输入作为替换数据?我们已经使用模板编辑器验证了替换数据。
transmission.setSubstitutionData(allSubstitutionData.asJava)
必填HTML:
{{offers[1].description}}
根据 documentation,在模板中循环遍历数组的方式是:
{{ if offers }}
<ul>
{{ each offer }}
<li>Offer title is <b>{{ loop_var.name }}</b></li>
{{ end }}
</ul>
{{ end }}
您需要使用变量 loop_var
,如果您在数组中传递一个对象,那么 loop_var
将是您对象的根。所以如果你想打印你的 discount
字段,你需要写 loop_var.discount
.
有很多关于如何做这类事情的示例 here。
对于您的具体情况,我认为您需要 this.
private void sendEmail(String from, String[] recipients) throws SparkPostException {
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
// Populate Recipients
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
for (String recipient : recipients) {
RecipientAttributes recipientAttribs = new RecipientAttributes();
recipientAttribs.setAddress(new AddressAttributes(recipient));
recipientArray.add(recipientAttribs);
}
transmission.setRecipientArray(recipientArray);
// Populate Substitution Data
Map<String, Object> substitutionData = new HashMap<String, Object>();
substitutionData.put("yourContent", "You can add substitution data too.");
transmission.setSubstitutionData(substitutionData);
// You can use Jackson, GSON or whatever you standard JSON decoding library is to
// Build this structure.
List<Map<String, String>> offers = new ArrayList<Map<String, String>>();
for (int i = 0; i < 2; i++) {
Map<String, String> offer = new HashMap<String, String>();
offer.put("description", "description value " + i);
offer.put("discount", "discount " + i);
offer.put("image", "image " + i);
offer.put("image_announcer", "image_announcer " + i);
offer.put("alt_title", "alt_title " + i);
offer.put("tracking", "tracking " + i);
offer.put("name", "name " + i);
offer.put("id", "id " + i);
offer.put("announcer_paid", "announcer_paid " + i);
offer.put("announcer_image", "announcer_image " + i);
offer.put("announcer_alt_title", "announcer_alt_title " + i);
offers.add(offer);
}
substitutionData.put("offers", offers);
// Populate Email Body
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
contentAttributes.setFrom(new AddressAttributes(from));
contentAttributes.setSubject("☰ Your subject content here. {{yourContent}}");
contentAttributes.setText("You could do it for text too. See https://www.sparkpost.com/blog/advanced-email-templates/ for an example");
contentAttributes.setHtml(
"<b>Your Data:</b><br>\n"
+ "<table border='1'>\n"
+ " <tr>\n"
+ " <th>description</th>\n"
+ " <th>discount</th>\n"
+ " <th>image</th>\n"
+ " <th>image_announcer</th>\n"
+ " <th>alt_title</th>\n"
+ " <th>tracking</th>\n"
+ " <th>name</th>\n"
+ " <th>id</th>\n"
+ " <th>announcer_paid</th>\n"
+ " <th>announcer_image</th>\n"
+ " <th>announcer_alt_title</th>\n"
+ " </tr>\n"
+ " {{each offers}} \n"
+ " <tr>\n"
+ " <td> {{{offers.description}}} </td>\n"
+ " <td> {{{offers.discount}}} </td>\n"
+ " <td> {{{offers.image}}} </td>\n"
+ " <td> {{{offers.image_announcer}}} </td>\n"
+ " <td> {{{offers.alt_title}}} </td>\n"
+ " <td> {{{offers.tracking}}} </td>\n"
+ " <td> {{{offers.name}}} </td>\n"
+ " <td> {{{offers.id}}} </td>\n"
+ " <td> {{{offers.announcer_paid}}} </td>\n"
+ " <td> {{{offers.announcer_image}}} </td>\n"
+ " <td> {{{offers.announcer_alt_title}}} </td>\n"
+ " </tr>\n"
+ " {{ end }} \n"
+ "</table>\n\n");
transmission.setContentAttributes(contentAttributes);
transmission.setContentAttributes(contentAttributes);
// Send the Email
IRestConnection connection = new RestConnection(this.client, getEndPoint());
Response response = ResourceTransmissions.create(connection, 0, transmission);
logger.debug("Transmission Response: " + response);
结果是这样的:
谢谢大家的回答。
我们在这里遇到的问题是从 Scala Map 类型到 Gson 的转换。
用Scala Maps创建的Gson库HashMaps处理的结果不一样。包括额外字段并更改 JSON.
的结构对于 Java 用户和 Scala,解决方案是
def toJavaConverter(objectLevelSubs: immutable.Map[String, AnyRef]): java.util.LinkedHashMap[String, Object] = {
val output = new java.util.LinkedHashMap[java.lang.String, Object]
objectLevelSubs.foreach {
case (k: String, v: List[Predef.Map[String, AnyRef]]) => output.put(k, v.map(toJavaConverter))
case (k: String, v: Predef.Map[String, AnyRef]) => output.put(k, toJavaConverter(v))
case (k: String, v: AnyRef) => output.put(k, v)
}
output}
最后像这样转换每个元素。
val gson: Gson = new GsonBuilder().setPrettyPrinting().enableComplexMapKeySerialization().create()
val finalSubstitutionData: util.LinkedHashMap[String, AnyRef] = new util.LinkedHashMap[String, AnyRef]()
javaObjectLevelSubs.forEach{
case (k: String, v: String) => finalSubstitutionData.put(k, v)
case (k: String, a) => a match {case l: List[_] => finalSubstitutionData.put(k, l.map(gson.toJsonTree).asJava)}
}
感谢@Yepher 和@balexandre