如何以速度在 JSON 数组中添加条目
howto add entries in JSON array with 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'}"
或者我可以修改一个值(例如'Index 1'.field2')吗?
感谢任何帮助
$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)
有了速度,我这样声明我的数组:
#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')吗?
感谢任何帮助
$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)