Documentation for the worlds leading Java Based Open Source Web Content Management System

dotCMS

JSON Objects

There are a variety of ways to glean information from external sites to display on your own. dotCMS provides tooling for many of the standard page/data display types on the web.

One option is to parse an xml file coming from an external site and user that information on your own site. dotCMS also supports the retrieval/serving of JSON objects which are less messy and easier to deal with than parsing an xml file.

The following two examples show how to deal with JSON Objects in dotCMS:

Retrieving an External JSON Object

This is an example of how to fetch a JSON object from an external site and display different object properties.

# set($myjson = $json.fetch("http://oohembed.com/oohembed/?url=http%3A//www.amazon.com/Myths-Innovation-Scott-Berkun/dp/0596527055/"))

The title is: $myjson.get("title")
The URL is: $myjson.get("url")
The author is: $myjson.get("author_name")

The results of the code above are:

The title is: The Myths of Innovation
The URL is: http://ecx.images-amazon.com/images/I/31h6%2Be-9KpL.jpg
The author is: Scott Berkun

Building and Serving your own JSON Objects

This is how to build a JSON object locally so that a JSON object can be served by your site. The JSON object values can be populated, for instance, with static or dynamic content from your dotCMS instance.

# set($mymap = $contents.getEmptyMap())
# set($dummy = $mymap.put("one","value 1"))
# set($dummy = $mymap.put("two","value 2"))
# set($dummy = $mymap.put("three","value 3"))
# set($myjson = $json.generate($mymap))

$myjson

The value of key "one" is $myjson.get("one")

The results of the code above are:

{"two":"value 2","one":"value 1","three":"value 3"} The value of key "one" is value 1

2

Subscribe to dotcms

Visit this group