• JSON basics and Liferay API

    Posted on September 26, 2014 by Hamidul Islam in Liferay.


    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 stands for JavaScript Object Notation
    • JSON is lightweight data interchange format
    • JSON is language independent

    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:

        {"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

    <h2>JSON Object Creation by JavaScript</h2>
    //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);

    JSON Array Creation by JavaScript:

    <h2>JSON Array Creation by JavaScript</h2>
    //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);

    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'}");

    Below are the imports



    Post Tagged with , ,
Comments are closed.
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.