On Sat, Jan 17, 2015 at 10:43 AM, Bryn Jeffries
<[email protected]> wrote:
> I'd like to be able to generate pure JSON from a wiki page, to use in other 
> pages via AJAX. To that end I created a page with the following content:
> {{velocity wiki="false"}}
> $response.setContentType('application/json')

> {
> "greeting" : "Hello",
> "location": "World"
> }

Instead of printing the JSON directly I recommend using the $jsontool
(see 
http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HVelocityTools
). The advantage is that you don't have to care about escaping JSON
(property) values.

$jsontool.serialize({
  "greeting" : $someObject.greeting,
  "location": $someOtherObject.location
})

Hope this helps,
Marius

> {{/velocity}}
>
> Calling this via /xwiki/bin/get/Sandbox/JsonTest I get the following code:
>
> <p><br/>&#123;<br/>"greeting" : "Hello",<br/>"location": "World"<br/>}</p>
>
> I get the same via /xwiki/bin/view/AlertDB/JsonTest?xpage=plain.
>
> If I remove the $response.setContentType('application/json')
> https://dev.alertnessdb.net/xwiki/bin/view/AlertDB/JsonTest?xpage=plain
>
>
> _______________________________________________
> users mailing list
> [email protected]
> http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________
users mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/users

Reply via email to