[xwiki-users] Need help with velocity+json
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'} }) How can I add an "Index2" entrie by example: 'Index2':{'Field1':'A new value','field2':'Another new value'}" and/or can how can I modify a value ('Index 1'.field2' by example)? I tried to generate arraw velocity variable from a string like this, but unfortunaly generate a unique ... string :-( Here my code: {{velocity}} #set ($MyArray={ 'Index1':{'Field1':'value1','field2':'value2'}, 'Index3':{'Field1':'A value','field2':'Another value'} }) $jsontool.serialize($MyArray) Correct json arraw but I can NOT edit it :-( {"Index1":{"Field1":"value1","field2":"value2"},"Index3":{"Field1":"A value","field2":"Another value"}} ---- #set ($MyArrayString="{") #set ($MyArrayString=$MyArrayString +"'Index1':{'Field1':'value1','field2':'value2'},") #set ($MyArrayString=$MyArrayString +"'Index3':{'Field1':'A value','field2':'Another value'}") #set ($MyArrayString=$MyArrayString +"}") $jsontool.serialize($MyArrayString) I can edit this json arraw but it as wrong json arraw because the "" characters :-( "{'Index1':{'Field1':'value1','field2':'value2'},'Index3':{'Field1':'A value','field2':'Another value'}}" {{/velocity}} Thxs for any help Pascal b
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
How can I add an "Index2" entrie by example:
'Index2':{'Field1':'A new value','field2':'Another new value'}"
and/or can how can I modify a value ('Index 1'.field2' by example)?
I tried to generate arraw velocity variable from a string like this,
but unfortunaly generate a unique ... string :-(
Here my code:
{{velocity}}
#set ($MyArray={ 'Index1':{'Field1':'value1','field2':'value2'}, 'Index3':{'Field1':'A value','field2':'Another value'} }) $jsontool.serialize($MyArray)
Correct json arraw but I can NOT edit it :-( {"Index1":{"Field1":"value1","field2":"value2"},"Index3":{"Field1":"A value","field2":"Another value"}}
----
#set ($MyArrayString="{") #set ($MyArrayString=$MyArrayString +"'Index1':{'Field1':'value1','field2':'value2'},") #set ($MyArrayString=$MyArrayString +"'Index3':{'Field1':'A value','field2':'Another value'}") #set ($MyArrayString=$MyArrayString +"}") $jsontool.serialize($MyArrayString)
I can edit this json arraw but it as wrong json arraw because the "" characters :-( "{'Index1':{'Field1':'value1','field2':'value2'},'Index3':{'Field1':'A value','field2':'Another value'}}"
{{/velocity}}
Thxs for any help
Pascal b _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
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
By the way, are there a "xwiki tricks" to pass a variable from velocity to javascript ? Anyways, this way working well (replace MyArray by MyMap ;-) ) : {{velocity}} {{html}} #set ($MyArray={ 'Index1':{'Field1':'value1','field2':'value2'}, 'Index3':{'Field1':'A value','field2':'Another value'} }) <script type="text/javascript"> var MaVariable=$jsontool.serialize($MyArray); </script> {{/html}} {{/velocity}} And in my XWiki.JavaScriptExtension object: function change_value(bouton, idTarget) { require(['jquery'], function ($) { // xxxxx var obj = MaVariable; alert(obj["Index2"]["field2"]); }); }; -------------------------------------------- En date de : Lun 27.7.15, Pascal BASTIEN <[email protected]> a écrit : Objet: Re: [xwiki-users] Need help with velocity+json À: "XWiki Users" <[email protected]>, "Marius Dumitru Florea" <[email protected]> Date: Lundi 27 juillet 2015, 12h01 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)
participants (2)
-
Marius Dumitru Florea -
Pascal BASTIEN