0

使用速度我声明我的数组是这样的:

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

如何通过示例添加“Index2”条目:

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

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

感谢您的帮助

4

1 回答 1

0

$MyArray 不是数组变量,而是映射(感谢 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)
于 2015-07-27T10:05:55.590 回答