首页 > 解决方案 > FreeMarker 模板错误:JSONArray 包装到 febStringModel

问题描述

我正在尝试将 JSON 对象发送到我的免费标记,但在测试 JUnit 时出现错误

这是我的对象

{
        "filename": "test",
        "orderId": "123435",
        "orderDate": "23.09.2020г.",
        "itemsCount": "4",
        "items": [
            {
                "itemName": "ТВ Приставка 400",
                "itemCount": "2 шт Х 400₽",
                "itemSum": "800.00"
            }
        ],
        "totalSumm": "3000.00"
    }

这是我的错误:

FreeMarker template error:
The value you try to list is an extended_hash+string (org.camunda.bpm.engine.impl.util.json.JSONArray wrapped into f.e.b.StringModel), thus you must specify two loop variables after the "as"; one for the key, and another for the value, like <#... as k, v>).

我该如何解决?

items 是 JSON 对象的数组

标签: javajunitfreemarker

解决方案


FreeMarker 对 . 一无所知org.camunda.bpm.engine.impl.util.json.JSONArray,因此它不会将其视为类似列表的事物(作为一个序列,因为它在 FTL 中被称为)。因此,您可以执行以下操作之一:

  • 使用该objectWrapper Configuration设置,您可以教 FreeMarker 如何将JSONArray其视为列表。(您可以在其他地方找到有关 custom ObjectWrapper-s 的更多信息。)然后您可以只#list使用此类对象,并使用适用于序列的所有其他操作。

  • JSONArray或者,您从模板调用 Java API-s 。当然,这会不太方便,但不需要事先投资。


推荐阅读