当前位置: 首页 > 知识库问答 >
问题:

如何从ftl中的json(从XML转换)中获取嵌套值

归明诚
2023-03-14

我在java中从json访问嵌套值时遇到了一些困难。让我首先告诉我的过程来做这件事。

首先,我以XML文件的形式获取输入,然后将其转换为JSONObject。下面是我从FTL生成HTML的尝试

HTMLMakerDemo

package com.imoveinvest.propertyuploader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.Writer;

import org.json.JSONObject;
import org.json.XML;
import org.springframework.stereotype.Service;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

@Service
public class PDFGenerationDemo implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static final String RESOURCES_DIR;
    public static final String OUTPUT_DIR;


    static {
        RESOURCES_DIR = "src//main//resources//";
        OUTPUT_DIR = "src//main//resources//output//";
    }

    public void makeHTML() throws IOException, TemplateException {
         String line = "", str = "";
         BufferedReader br = new BufferedReader(new FileReader(RESOURCES_DIR +
         "sample.xml"));
         while ((line = br.readLine()) != null) {
         str += line;
         }
         JSONObject root = XML.toJSONObject(str);
         JSONObject section = (JSONObject)root.get("property-valuation-report");
         section.put("data", section);
         PDFGenerationDemo pdf = new PDFGenerationDemo();
         pdf.makeTemplate(section);
         }
    public void makeTemplate(JSONObject obj) throws TemplateException{
        Configuration cfg = new Configuration();
        cfg.setObjectWrapper(new JSONArrayObjectWrapper());
        cfg.setClassForTemplateLoading(this.getClass(), "/templates/");


        try {
            Template sampleTemplate= cfg.getTemplate("sampleTemplate.ftl");
            Writer consoleWriter = new OutputStreamWriter(System.out);
            sampleTemplate.process(obj, consoleWriter);
            Writer fileWriter = new FileWriter(new File("output.html"));
            sampleTemplate.process(obj, fileWriter);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我的FTL是

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <h1>Deserialization with Freemarker Hash</h1>
        <#assign obj = data.section>
        <#list obj as item>
            <#if item.name=="valuation">
                <div>${item.capital-value}</div>
            </#if>
        </#list>
    </body>
</html>

为了消除JSONparsing错误,我从下面这样做https://stackoverflow.com/a/51876242/12876529

package com.imoveinvest.propertyuploader;

import org.json.JSONArray;
import org.json.JSONException;

import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateSequenceModel;

@SuppressWarnings("deprecation")
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();
        }

    }


}

所以我的问题是在我的XML文件中,我有像capital value这样的标记

<?xml version="1.0" encoding="utf-8"?>
<sample-data xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:ht="urn:hometrack.com/xslt/extensions">
    <section name="valuation">
        <capital-value>
            <from>855000</from>
            <to>1045000</to>
            <accuracy>medium</accuracy>
            <value>950000</value>
            <valuation-date>2020-02-12T00:00:00</valuation-date>
        </capital-value>
        <last-recorded-sale />
        <rental-value>
            <value>2720</value>
            <yield>3.4357894736842107</yield>
        </rental-value>
        <location>
            <lat>51.611573777232579</lat>
            <long>-0.32247146663868992</long>
        </location>
    </section>
</sample-data>

所以当你看我的ftl时,你会看到我写了

<div>${item.capital-value}</div>

这给了我一个错误

freemarker.core._TemplateModelException: An error has occurred when reading existing sub-variable "capital"; see cause exception! The type of the containing value was: extended_hash+string (org.json.JSONObject wrapped into f.e.b.StringModel)

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${item.capital - value}  [in template "sampleTemplate.ftl" at line 8, column 38]
----
    at freemarker.ext.beans.BeanModel.get(BeanModel.java:186)
    at freemarker.core.Dot._eval(Dot.java:43)
    at freemarker.core.Expression.eval(Expression.java:81)
    at freemarker.core.Expression.evalToNumber(Expression.java:118)
    at freemarker.core.ArithmeticExpression._eval(ArithmeticExpression.java:51)
    at freemarker.core.Expression.eval(Expression.java:81)
    at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:96)
    at freemarker.core.DollarVariable.accept(DollarVariable.java:59)
    at freemarker.core.Environment.visit(Environment.java:326)
    at freemarker.core.Environment.visit(Environment.java:368)
    at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:315)
    at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271)
    at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:242)
    at freemarker.core.Environment.visitIteratorBlock(Environment.java:595)
    at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:107)
    at freemarker.core.IteratorBlock.accept(IteratorBlock.java:93)
    at freemarker.core.Environment.visit(Environment.java:326)
    at freemarker.core.Environment.visit(Environment.java:332)
    at freemarker.core.Environment.process(Environment.java:305)
    at freemarker.template.Template.process(Template.java:378)
    at com.imoveinvest.propertyuploader.PDFGenerationDemo.makeTemplate(PDFGenerationDemo.java:57)
    at com.imoveinvest.propertyuploader.PDFGenerationDemo.makeHTML(PDFGenerationDemo.java:46)
    at com.imoveinvest.propertyuploader.controller.SaveOrValidateValuationReport.htmlCall(SaveOrValidateValuationReport.java:152)
    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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException: null
    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 freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:1487)
    at freemarker.ext.beans.BeanModel.invokeGenericGet(BeanModel.java:268)
    at freemarker.ext.beans.BeanModel.get(BeanModel.java:155)
    ... 64 common frames omitted
Caused by: org.json.JSONException: JSONObject["capital"] not found.
    at org.json.JSONObject.get(JSONObject.java:473)
    ... 71 common frames omitted
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <h1>Deserialization with Freemarker Hash</h1>
                        <div>FreeMarker template error (DEBUG mode; use RETHROW in production!):

共有1个答案

伊富
2023-03-14

您需要将属性名称“capital value”中的连字符转义

仅当您使用2.3.22或更高版本的freemarker时。请参见freemarker变量语法:

在这种表达式中,变量名只能包含字母(包括非拉丁字母)、数字(包括非拉丁数字)、下划线(\uu)、美元($)、符号(@)。此外,第一个字符不能是ASCII数字(0-9)。从FreeMarker 2.3.22开始,变量名还可以包含减号(-)、点(.),和冒号(:),但必须用前面的反斜杠()转义,否则它们将被解释为运算符。

 类似资料:
  • 然后连空白节点都是嵌套的,这是我不想要的。有什么方法可以使用Jena获得这种结构吗?或者有没有其他的Java库可以更好地处理这一点?

  • 我将json数据流(从ConvertAvroToJSON处理器驱动)转义为: 我只需要“JSON”值,如下所示[unescapeJson之后]: $...*

  • 我有下一个带嵌套括号的字符串: 我想用打印出来的每个括号的值填充一个数组:

  • 我试图有一个键值对映射到一个数组,区分每个值作为一个类型使用jolt转换规范 输入json 尝试了这个规格

  • 问题内容: 我在下面的对象中尝试获取所有id值。 使用以下代码,我仅获得第一个id值。有没有办法从嵌套对象中获取所有id值,而无需使用任何外部模块。 预期产量 问题答案: 您可以使用如下所示的JavaScript函数来获取嵌套属性: 检查此小提琴以获取可行的解决方案。