【问题标题】:howto add entries in JSON array with velocity如何以速度在 JSON 数组中添加条目
【发布时间】:2015-10-15 16:47:48
【问题描述】:

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

#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')?

感谢任何帮助

【问题讨论】:

    标签: velocity xwiki


    【解决方案1】:

    $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)
    

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 2019-08-25
      • 1970-01-01
      • 2015-01-13
      • 2011-05-31
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多