Site icon Pro Liferay

JSON basics and Liferay API

json

 
Aim of this Article:
In this article I will explain the basics of JSON and how we can create JSON using Liferay API.

Whats JSON:

JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999.

Example of JSON Object:


{"name":"Hamidul Islam","country":"India","phone":"8095185442"}


Note 1: A JSON Object is formed in between {  }

Note 2: A JSON Object can contains many data. Each data is separated by comma(,). For example in the above example  “name”:”Hamidul Islam” is the data.

Note 3: Each data is consist of key and value pair. In “name”:”Hamidul Islam” name is the key and Hamidul Islam is the value.


Example of JSON Array:

"persons":[
    {"firstName":"Hamidul", "lastName":"Islam"},
    {"firstName":"John", "lastName":"Abraham"},
    {"firstName":"Raja", "lastName": "Kunal"}
];

Note 1: A [  ] represents JSON Array. In the above example the name of the array is persons

Note 2: A JSON Array contains multiple Objects. Each object in the array is represented by {  }. In the above example there are 3 JSON Objects inside the array.

 


 

JSON object creation by JavaScript:

Anything can not be converted to JSON Object. A string of JSON format can be converted to JSON object by  using  JSON.parse

<html>
<body>
<h2>JSON Object Creation by JavaScript</h2>
<script>

//Below is the JSON String
var jsonString = '{"name":"Hamidul Islam","country":"India","phone":"8095185442"}'

//Below is the JSON Object
var jsonObject = JSON.parse(jsonString);

//Access value from JSON Object
alert("Value of name:"+jsonObject.name);
alert("Value of country:"+jsonObject.country);
</script>
</body>
</html>

JSON Array Creation by JavaScript:

<html>
<body>
<h2>JSON Array Creation by JavaScript</h2>
<script>
//Create the JSON Array
var persons = [
    {"firstName":"Hamidul", "lastName":"Islam"},
    {"firstName":"John", "lastName":"Abraham"},
    {"firstName":"Raja", "lastName": "Kunal"}
];
//Access the JSON Object inside the array
alert("Value of firstName in the first object:"+persons[0].firstName);
alert("Value of lastName in the first object:"+persons[0].lastName);
</script>
</body>
</html>

Note: While creating JSON Array don’t use JSON.parse

Liferay API to create JSON Object:

Consider we need to create JSON object like {“name”:”Hamidul Islam”,”country”:”India”}. This can be done in 2 ways

First Option:

JSONObject jsonObject =  JSONFactoryUtil.createJSONObject();
jsonObject.put("name", "Hamidul Islam");
jsonObject.put("country", "India");

Second Option:

String jsonString = "{'name':'Hamidul Islam','country':'India'} ";
JSONObject jsonObject = JSONFactoryUtil.createJSONObject(jsonString);

Liferay API to create JSON Array:

Suppose we want to create a JSON Array

 [{“name”:”Hamidul Islam”,”country”:”India”},{“name”:”Jacob”,”country”:”Canada”},{“name”:”Smith”,”country”:”US”}]

Below is the code

JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
JSONObject jsonObject1 = JSONFactoryUtil.createJSONObject("{'name':'Hamidul Islam','country':'India'}");
JSONObject jsonObject2 = JSONFactoryUtil.createJSONObject("{'name':'Jacob','country':'Canada'}");
JSONObject jsonObject3 = JSONFactoryUtil.createJSONObject("{'name':'Smith','country':'US'}");
jsonArray.put(jsonObject1);
jsonArray.put(jsonObject2);
jsonArray.put(jsonObject3);

Below are the imports

com.liferay.portal.kernel.json.JSONArray
com.liferay.portal.kernel.json.JSONFactory
com.liferay.portal.kernel.json.JSONObject
com.liferay.portal.kernel.json.JSONFactoryUtil

http://www.w3schools.com/json/

Exit mobile version