Thxs a lot: "put" is my new friend :-) Here the answer: #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) -------------------------------------------- En date de : Lun 27.7.15, Marius Dumitru Florea <[email protected]> a écrit : Objet: Re: [xwiki-users] Need help with velocity+json À: "Pascal BASTIEN" <[email protected]>, "XWiki Users" <[email protected]> Date: Lundi 27 juillet 2015, 10h47 Hi Pascal, On Mon, Jul 27, 2015 at 11:42 AM, Pascal BASTIEN <[email protected]> wrote:
Hello,
My goal is to send array json variable from velocity to javacript or more precisely I want loop over all objects of same Class of a page and put them in a json array (http://platform.xwiki.org/xwiki/bin/view/DevGuide/APIGuide#HAccessobjectsfro...)
But I didn't manage to edit an velocity array variable (add an entries or modify one) AND
With velocity I declare my array like this:
#set ($MyArray={ 'Index1':{'Field1':'value1','field2':'value2'}, 'Index3':{'Field1':'A value','field2':'Another value'} })
This is not an array but a Map http://docs.oracle.com/javase/7/docs/api/java/util/Map.html , which is quite different. In any case, when you're not sure about the type of a Velocity variable you should print $myVar.getClass().getName() to see the real Java type and then you should check the Javadoc for that type to see what methods you can call on the variable. I'll let you discover how to modify a Java map object. Hope this helps, Marius