如何在FTL文件中读取JSONArray?

时间:2018-08-16 11:13:16

标签: java json freemarker

我将下面的JSON对象硬编码在我的Java文件中

    JSONObject notificationInfoJson = new JSONObject();
    notificationInfoJson.put("title", "Payment Received");
    notificationInfoJson.put("firstName", "Bhuvan");
    notificationInfoJson.put("lastName", "Aggarwal");
    notificationInfoJson.put("accountId", "111");
    notificationInfoJson.put("paymentId", "555");

    JSONArray accounts = new JSONArray();
    for(int i=1; i<=2; i++) {
      JSONObject account = new JSONObject();
      account.put("accountId", 1000 + i);
      account.put("paymentId", 1000 + i);
      accounts.put(account);
   }    
    notificationInfoJson.put("accounts", accounts);

// passing the below json in the template as input
        JSONObject data = new JSONObject();
        data.put("notificationInfo", notificationInfoJson);

Json形成为:

{
  "title": "Payment Received",
  "firstName": "Bhuvan",
  "lastName": "Aggarwal",
  "accountId": "111",
  "paymentId": "555",
  "accounts": [
    {
      "accountId": 1001,
      "paymentId": 1001
    },
    {
      "accountId": 1002,
      "paymentId": 1002
    }
  ]
}

能否请您告诉我如何读取FTL文件中的Json数组?

FTL文件:

<!doctype html>
<html lang="en">
    <head>
        <title>Notification</title>
    </head>
    <body>
        <div class="page">
            <p>Dear ${notificationInfo.firstName} ${notificationInfo.lastName}</p>
            <p>Your payment has been received for the following accounts</p>

            <#list notificationInfo.accounts?keys as account>
                <p>Account ID: ${account.accountId}</p>
                <p>Payment ID: ${account.paymentId}</p>
            </#list>

            <br>
            <p>Thank you!</p>
        </div>
    </body>
</html>

错误:

2018-08-16 16:59:29,929 [                          main] runtime                        ERROR Error executing FreeMarker template
FreeMarker template error:
For "." left-hand operand: Expected a hash, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> account  [in template "payment-received.ftl" at line 12, column 34]

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${account.accountId}  [in template "payment-received.ftl" at line 12, column 32]
----

Java stack trace (for programmers):
----
freemarker.core.NonHashException: [... Exception message was already printed; see it above ...]
    at freemarker.core.Dot._eval(Dot.java:45)
    at freemarker.core.Expression.eval(Expression.java:78)
    at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
    at freemarker.core.DollarVariable.accept(DollarVariable.java:41)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.MixedContent.accept(MixedContent.java:54)
    at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
    at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:240)
    at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220)
    at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:194)
    at freemarker.core.Environment.visitIteratorBlock(Environment.java:572)
    at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78)
    at freemarker.core.IteratorBlock.accept(IteratorBlock.java:64)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.MixedContent.accept(MixedContent.java:54)
    at freemarker.core.Environment.visit(Environment.java:324)
    at freemarker.core.Environment.process(Environment.java:302)
    at freemarker.template.Template.process(Template.java:325)
    at com.amdocs.bil.notification.beans.TemplateEngine.generateMessage(TemplateEngine.java:125)
    at com.amdocs.bil.notification.TestTemplateEngine.testTemplateEngine(TestTemplateEngine.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
2018-08-16 16:59:29,936 [                          main] TemplateEngine                 ERROR TemplateEngine.generateMessage: freemarker.core.NonHashException: For "." left-hand operand: Expected a hash, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> account  [in template "payment-received.ftl" at line 12, column 34]

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${account.accountId}  [in template "payment-received.ftl" at line 12, column 32]
----

3 个答案:

答案 0 :(得分:1)

org.codehaus.jettison.json.JSONArray不会扩展/实现任何“标准”类型(例如java.util.List),因此,即开即用的FreeMarker不会将其视为可列出的值。但是,模板并不直接依赖java.util.List等,而是依赖TemplateModel -s。在FreeMarker中,有一个ObjectWrapper可以将所有对象包装到TemplateModel -s中,而模板只能看到被包装的对象。因此,可列出的内容由ObjectWrapper决定。因此,解决方案的步骤是:

  1. 实施一个TemplateSequenceModel,该JSONArray包装(委托给)WrapperTemplateModel。 (有时也可以实现AdapterTemplateModelfreemarker.template.DefaultObjectWrapper,但这是可选的。)

  2. 扩展handleUnknownType并覆盖JSONArray,如果得到TemplateSequenceModel,则将其包装到handleUnknownType实现中。 (例如,请参见DefaultObjectWrapper本身的object_wrapper方法。)

  3. 将配置的Configuration.setObjectWrapper设置(例如通过ObjectWrapper)设置为<#list notificationInfo.accounts as account>类的实例。

现在Fragment fragment = new tasks(); FragmentManager fragmentManager = context.getSupportFragmentManager(); // this is the context of the Activity FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Bundle bundle=new Bundle(); bundle.putString("name", "Osmar Cancino"); //key and value //set Fragmentclass Arguments fragment.setArguments(bundle); fragmentTransaction.replace(R.id.content_frame, fragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); 应该可以工作了。

答案 1 :(得分:0)

我发现必须在FTL文件中使用以下代码

  

notificationInfo.accounts将调用JSONObject.get(“ accounts”),如下   FreeMarker知道get(key)约定。结果是   JSONArray。但这并没有实现List或任何熟悉的方法,因此   FreeMarker不知道如何列出它。

使用以下代码:

<#assign accounts = notificationInfo.accounts>
<#list 0 ..< accounts.length() as i>
   <p>Account ID: ${accounts.get(i).accountId} </p>
   <p>Payment ID: ${accounts.get(i).paymentId} </p>
</#list>

OR

更好的方法是编写CustomObjectWrapper并在配置中设置ObjectWrapper。感谢@ddekany的建议

cfg.setObjectWrapper(new JSONArrayObjectWrapper());

    public class JSONArrayObjectWrapper extends DefaultObjectWrapper {

    @Override
    public TemplateModel handleUnknownType (Object obj) throws TemplateModelException {

        if (obj instanceof JSONArray) {
            return new JSONArraySequenceModel((JSONArray) obj);
        }

        return super.handleUnknownType(obj);
    }


    public class JSONArraySequenceModel implements TemplateSequenceModel {

        private JSONArray jsonArray;

        public JSONArraySequenceModel(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }

        @Override
        public TemplateModel get(int index) throws TemplateModelException {
            TemplateModel model = null;
            try {

                model = wrap(jsonArray.get(index));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return model;
        }

        @Override
        public int size() throws TemplateModelException {
            return jsonArray.length();
        }

    }


}

答案 2 :(得分:0)

Json输入文件:

{
  "title": "Payment Received",
  "firstName": "vijay",
  "lastName": "dwivedi",
  "accountId": "123",
  "paymentId": "456",
  "accounts": [
    {
      "accountId": 0001,
      "paymentId": 1001
    },
    {
      "accountId": 0002,
      "paymentId": 2002
    },
    {
      "accountId": 0003,
      "paymentId": 3003
    }
  ]
}

Smook_config文件:

要访问json数组,需要使用 array_node.element

 <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:json="http://www.milyn.org/xsd/smooks/json-1.1.xsd" xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">

        <params>
            <param name="stream.filter.type">SAX</param>
            <param name="default.serialization.on">false</param>
        </params>

        <json:reader rootName="json" keyWhitspaceReplacement="_">
            <json:keyMap>
                <json:key from="date&amp;time" to="date_and_time" />
            </json:keyMap>
        </json:reader>

        <resource-config selector="json">
            <resource>org.milyn.delivery.DomModelCreator</resource>
        </resource-config>

        <ftl:freemarker applyOnElement="json"> *<!-- Json is Root Element for request -->*
            <ftl:template>
                <!--
                    <#list json.accounts.element as ali_accounts>

                          "accountId": "${ali_accounts.accountId}"
                          "paymentId": "${ali_accounts.paymentId}"

                    </#list>

                --> 
            </ftl:template>
        </ftl:freemarker>

</smooks-resource-list>