2016-04-12 123 views
1

我有一個json響應,我使用Apache Valocity模板將其轉換爲xml。我的JSON的格式如下:使用Apache Velocity模板的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"] 
     } 

    } 
} 

我Velocity模板的格式如下:

<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 |  

得到任何想法?

+0

請檢查速度模板中使用的所有變量是否也在速度模型中。不確定,在第一個foreach中,「$ {body.value}」是正確的。如果body在你的模型中,也嘗試使用getter在模板中訪問它,如$ body.getValue() – dumitru

+0

嗨,實際上我正在獲取值。 –

回答

0

我認爲這可能是一個錯字,但我們錯過了#end。 (上thirdtag的foreach) 嘗試用這個模板:

<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> 
+0

感謝您指出缺少'#end'標籤,這是一個錯字。你不認爲第二個foreach塊不應該在映射ThirdTag後結束? –

+0

這取決於您的業務邏輯。你決定根據你想要的輸出。如果答案有助於您將其標記爲有用。 – dumitru

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>