[英]How to access json value by key value in freemarker?
Is there any way to access a json
value by key value in freemarker?有没有办法通过freemarker中的键值访问json
值? The json looks like this: json 看起来像这样:
{"className":"Lorem Ipsum","classPK":"52293","title":"Test Name"}
I need these values to use them in a method:我需要这些值才能在方法中使用它们:
localService.method(className, classPK);
I was trying to access them directly with ${json.getData()["classPK"]}
.我试图用${json.getData()["classPK"]}
直接访问它们。 How to do it?怎么做?
You can use by using ${variableName.key}
.您可以使用${variableName.key}
。 In Your case <#assign data="Your JSON Goes here"?eval>
then after access using ${data.className}
.在您的情况下<#assign data="Your JSON Goes here"?eval>
然后在使用${data.className}
访问之后。
Here Using ?eval
on String data which includes JSON ,we convert it to Freemarker littral.这里对包含 JSON 的字符串数据使用?eval
,我们将其转换为 Freemarker littral。
For more information follow the links:- https://docs.akana.com/ag/processes/process_transform_freemarker.htm https://liferay.dev/blogs/-/blogs/working-with-json-in-freemarker有关更多信息,请访问以下链接:- https://docs.akana.com/ag/processes/process_transform_freemarker.htm https://liferay.dev/blogs/-/blogs/working-with-json-in-freemarker
You can either use ?eval or the jsonFactoryUtil (if available).您可以使用 ?eval 或 jsonFactoryUtil(如果可用)。 Examples:例子:
<#assign customFieldJson = customField.getData()?eval />
${customFieldJson.className}
${customFieldJson.classPK}
${customFieldJson.title}
or或者
<#assign customFieldJson = jsonFactoryUtil.createJSONObject(customField.getData()) />
${customFieldJson.getString("className")}
${customFieldJson.getString("classPK")}
${customFieldJson.getString("title")}
I don't know which one is "better", but i prefer eval...我不知道哪个“更好”,但我更喜欢 eval...
Assign json into this variable and try to access this way将json赋值给这个变量,尝试这样访问
<#-- Freemarker hash from JSON literal --> <#assign getData= {"className":"Lorem Ipsum","classPK":"52293","title":"Test Name"} > <#-- 来自 JSON 文字的 Freemarker 哈希 --> <#assign getData= {"className":"Lorem Ipsum","classPK":"52293","title":"Test Name"} >
${getData.className} ${getData.classPK} ${getData.className} ${getData.classPK}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.