如何使用velocity在JSON数组中添加条目

时间:2015-07-24 21:42:30

标签: velocity xwiki

使用velocity我会像这样声明我的数组:

#set ($MyArray={
  'Index1':{'Field1':'value1','field2':'value2'},
  'Index3':{'Field1':'A value','field2':'Another value'}
})

如何添加" Index2"以身作则:

'Index2':{'Field1':'A new value','field2':'Another new value'}"

或者我可以修改一个值('索引1' .field2'例如)?

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

$ MyArray不是数组变量,而是地图(Thxs to Marius): http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

然后答案是:

#set ($MyArray={
  'Index1':{'Field1':'value1','field2':'value2'},
  'Index3':{'Field1':'A value','field2':'Another value'}
})
$MyArray.getClass().getName()
* Original map
$jsontool.serialize($MyArray)
* I Modify the 'Index3' entrie:
#set ($discard=$MyArray.put('Index3',{'Field1':'A modified value','field2':'Another modified value'}))
$jsontool.serialize($MyArray)
* I add the 'Index2' entrie:
#set ($discard=$MyArray.put('Index2',{'Field1':'Addon value','field2':'Another addon value'}))
$jsontool.serialize($MyArray)