簡體   English   中英

使用Apache Velocity模板進行Json映射

[英]Json Mapping using Apache Velocity Template

我有一個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  |     

有任何想法嗎?

我認為這可能是拼寫錯誤,但我們缺少#end。 (有關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>

所以在沖了一段時間之后。 我想出了合適的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM