2013-08-26 31 views
1

我在這個格式的JSON數組:
如何遍歷freemarker中的JSON返回數組?

{ 
"productList" : [ 
{ 
     "nodeRef"   : "workspace:\/\/SpacesStore\/55f8dd88-dccd-4c09-9a67-bb3f2d5f02d4", 
    "photoProduit"  : "api\/node\/workspace\/SpacesStore\/60f80999-0d2b-4798-abbb-786d5fe13a85\/content\/thumbnails\/doclib", 
    "typeProduit"  : "Bonbon", 
    "uniteQuantite"  : "48", 
    "tarifUnitaire"  : "1.89", 
    "descriptionProduit": "Sweets product update test 2" 
} 
     , 
{ 
     "nodeRef"   : "workspace:\/\/SpacesStore\/c2ffb3c1-9091-47fb-9b01-ce6b0d30a869", 
    "photoProduit"  : "api\/node\/workspace\/SpacesStore\/77264862-54a4-4248-acf0-8e36c400dc1a\/content\/thumbnails\/doclib", 
    "typeProduit"  : "Post It Notes", 
    "uniteQuantite"  : "10", 
    "tarifUnitaire"  : "2.76", 
    "descriptionProduit": "Post It Notes" 
} 

] 
} 

我不確定我怎麼能訪問數組元素作爲productList的是哈希值。是否有可能在freemarker中解析這個,因爲Hashes沒有很多內置的插件,我似乎無法將序列內置插件應用到數據中。

回答

4

productList不是散列,它是一個序列(一個列表)。所以,如果你在FreeMarker的變量productListproductList,你可以這樣做:

<#list productList as product> 
    ${product.nodeRef} 
</#list> 
+0

未不幸的是工作。這是我的一個疏忽。我忘了我必須通過在我的應用程序中分配返回的模型對象來訪問它。 –

+0

我接受這個作爲這個小附錄的答案,以幫助解釋其他人被抓出來的情況;因爲返回被分配給根作用域對象(在我的例子中,這也被命名爲「productList」),正確的語法是使用 [「productList」];所以<#list productList [「productList」] as product更具體 –

+0

如果您只在頂級JSON映射中包含'productList',那麼將其添加到數據模型而不是映射本身。或者,使用地圖本身作爲數據模型。在這兩種情況下,您都應避免在'productList'中使用'productList'。順便說一句,你可以把'productList [「productList」]寫成'productList.productList'。 – ddekany