On Fri, May 24, 2013 at 10:54 AM, Marius Dumitru Florea <[email protected]> wrote:
On Fri, May 24, 2013 at 10:54 AM, <[email protected]> wrote:
Hi everybody,
I am new at XWiki and I would like to know if it's possible to build a JSON object in a velocity script and get later this JSON via the REST API.
This is my script :
{{velocity}}
#if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') $response.setContentType('application/json') #end
#set($hql = "") #set($results = $xwiki.searchDocuments($hql)) #set($json= "[" ) #set($started = false) #foreach($item in $results) #set($itemdoc = $xwiki.getDocument($item)) #if($started) #set($pointvirgule=",") #set($json = "${json} ${pointvirgule}" ) #else #set($started = true) #end
#set($suite="{ ""name"" : ""${itemdoc.fullName}""}") #set($json= "${json} ${suite}")
#end
#set($fin="]") #set($json= "${json} ${fin}")
$json
It is advisable to build the JSON in memory using objects like maps, arrays, etc. and then serialize the 'root' object using the JSON Velocity tool http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HJSONTo... . One of the advantages with this approach is that you don't have to care about escaping JSON values (in your code above any of the strings you put in the JSON can contain special characters that would break the JSON).
I would add that you should use {{velocity wiki="false"}} as otherwise your JSON is going to be parsed as wiki syntax.
And you don't need REST to access the generated JSON. Just use the page URL (/xwiki/bin/get/Space/Page?outputSyntax=plain).
Hope this helps, Marius
{{/velocity}}
I thought my JSON would be in the content of my page but when I try to get this content with REST API I only have the velocity script in hard.
Someone could help me ?
Thanks in advance for your answers.
Pierrick
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne