使用Apache Velocity模板进行Json映射

时间:2016-04-12 06:46:13

标签: java json apache mapping velocity

我有一个json响应,我正在使用Apache Valocity模板转换为xml。我的json采用以下格式:

{
    "value": {
        "1234": {
            "7": ["abcde"],
            "2": ["abcde", "lkisy", "skjsk"],
            "1": ["aksdja", "msnd", "knsda"]
        },
        "7654": {
            "8": ["NA"],
            "4": ["NA"],
            "7": ["BA"]
        },
        "0987": {
            "8": ["PO"],
            "3": ["LO"],
            "2": ["KI"]
        }

    }
}

我的力度模板采用以下格式:

<VelocityResponse xmlns="xsd location namespace">
<HeaderID>${headers.HeaderID}</HeaderID>
<FirstTag>
#foreach($body in ${body.value})
<SecondTag>
#set($keys = $body.keySet())
#foreach($key in $keys)
<ThirdTag>$key</ThirdTag>
#set($someValue = $key.keySet())
#foreach($value in $someValue)
<FourthTag>$value</FourthTag>
<FifthTag>
#foreach($userInfo in $someValue[$value])
<SixthTag>$userInfo</SixthTag>
#end
</FifthTag>
#end
</SecondTag>
#end
</FirstTag>
</VelocityResponse>

当我尝试映射时,我收到了这个错误:

198 - org.apache.camel.camel-core - 2.15.1.redhat-621084 | Error : org.apache.velocity.exception.ParseErrorException: Encountered "<EOF>" at org.apache.camel.component.velocity.VelocityEndpoint[line 21, column 27]
n  | Was expecting one of:
n  |     "(" ...
n  |     <RPAREN> ...
n  |     <ESCAPE_DIRECTIVE> ...
n  |     <SET_DIRECTIVE> ...
n  |     "##" ...
n  |     "\\\\" ...
n  |     "\\" ...
n  |     <TEXT> ...
n  |     "*#" ...
n  |     "*#" ...
n  |     "]]#" ...
n  |     <STRING_LITERAL> ...
n  |     <END> ...
n  |     <IF_DIRECTIVE> ...
n  |     <INTEGER_LITERAL> ...
n  |     <FLOATING_POINT_LITERAL> ...
n  |     <WORD> ...
n  |     <BRACKETED_WORD> ...
n  |     <IDENTIFIER> ...
n  |     <DOT> ...
n  |     "{" ...
n  |     "}" ...
n  |     <EMPTY_INDEX> ...
n  |     

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我认为这可能是一个错字,但我们错过了#end。 (foreach on thirdtag) 试试这个模板:

<VelocityResponse xmlns="xsd location namespace">
    <HeaderID>${headers.HeaderID}</HeaderID>
    <FirstTag>
        #foreach($body in ${body.value})
        <SecondTag>
            #set($keys = $body.keySet())
            #foreach($key in $keys)
                <ThirdTag>$key</ThirdTag>
            #end
            #set($someValue = $key.keySet())
            #foreach($value in $someValue)
            <FourthTag>$value</FourthTag>
            <FifthTag>
                #foreach($userInfo in $someValue[$value])
                <SixthTag>$userInfo</SixthTag>
                #end
            </FifthTag>
            #end
        </SecondTag>
        #end
    </FirstTag>
</VelocityResponse>

答案 1 :(得分:0)

所以经过一段时间的头脑风暴后。我提出了正确的xml结构:

<VelocityResponse xmlns="xsd location namespace">
<HeaderID>${headers.HeaderID}</HeaderID>
<FirstTag>
#foreach($body in ${body.value})
#set($keys = $body.keySet())
#foreach($key in $keys)
<SecondTag>
<ThirdTag>$key</ThirdTag>
<NewTag>
#set($somevalue = $body[$key].keySet())
#foreach($input in $somevalue)
<FourthTag>$input</FourthTag>
<FifthTag>
#foreach($userInfo in $body[$key][$input])
<SixthTag>$userInfo</SixthTag>
#end
</FifthTag>
#end
</NewTag>
</SecondTag>
#end
#end
</FirstTag>
</VelocityResponse>