簡體   English   中英

使用 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}}

根據文檔,在模板中循環數組的方式是:

{{ 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

有很多的樣品怎么辦之類的事情在這里

為了您的具體情況,我想你想是這樣

      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 的這個答案:首先迭代所有轉換為 Java 類型的映射,如下所示:

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM