速度循环通过 JSON

Velocity loop through JSON

我正在使用 Velocity 尝试循环遍历我在 Marketo 的一个字段中的一些 JSON 数据。我已经使用 the tutorial here 将 JSON 更改为 Velocity Map 但在尝试遍历现在是地图的数据时遇到问题。

#set( $items = ${abandonedBasket_cList} )

###if( $items.get(7).basketJSON.isEmpty() )
###set( $items.get(7).basketJSON = '[]' )
###end

#foreach ($item in $items)
    #if( $foreach.last )
        #set( $lastitem = $item.basketJSON )
    #end 
#end

#set( $ClientReferrals = '#set( $ClientReferrals = ' + $lastitem + ' ) ' )
#evaluate( $ClientReferrals )


${ClientReferrals[0].itemDesc}       ## outputs "Season Pass Holders"
<br>
${ClientReferrals[1].customerID}     ## outputs "jill@example2.com"
<br>

当使用示例中的特定静态索引时,我已经管理了“地图”的输出值。

我坚持的一点是尝试使用 Velocity 中的 foreach 工具让它遍历每个对象,因为我不知道如何在循环中迭代 [index] 键。

更新

我已尝试使用建议的代码,但没有显示任何内容。

#foreach($key in $ClientReferrals.keySet())
   #set($item = $ClientReferrals[$key])
   $item.itemDesc
   ${item.itemDesc}
   $item
   ${item}
#end

如果我输出 $ClientReferrals 的内容,这就是我得到的。这看起来像 Hashmap 吗?

[{id=29071940, itemDesc=1.33ct AA Tanzanite 9K Gold Earrings, itemImage=https://cdn.gemporia.com/images/products/300/NJPS19.jpg, itemQuantity=1, itemPrice=£151.00, customerID=1132399, basketURL=https://secure.gemporia.com/basket.aspx, isAuction=false, stock=10, dateAdded=2017-02-15}, {id=29071946, itemDesc=Size J to K AA Tanzanite & White Zircon 9K Gold Ring ATGW 1.08cts, itemImage=https://cdn.gemporia.com/images/products/300/OZVJ80.5.jpg, itemQuantity=1, itemPrice=£89.99, customerID=1132399, basketURL=https://secure.gemporia.com/basket.aspx, isAuction=true, stock=1, dateAdded=2017-02-15}] 

如果$ClientReferrals是一张地图,那么你可以这样做:

#foreach($key in $ClientReferrals.keySet())
   #set($item = $ClientReferrals[$key])
   ...
#end

HashMap 不会按键排序项目,TreeMap 会按键排序。

顺便说一下,要获取数组的最后一项,您可以这样做:

#set($last_index = $items.size() - 1)
#set($last = $items[$last_index])