Java

Building a JSON Object using put(), String and java.util.Map

There are multiple ways if creating q JSON Object, using various constructor of JSONObject

  1. JSONObject() -- creates a blank JSON Object and can be filled up using put() methods

  2. JSONObject(String jsonString) -- creates a JSONObject based upon the provided String representation of JSON.

  3. JSONObject(java.util.Map map) -- creates a JSONObject based upon the provided map object.

Lets create a simple JSON Object using put methods

import org.json.*;


class MakeAJSON
{
    public static void main(String[] args)
    {
        JSONObject obj=new JSONObject();
        obj.put("name","Code with Z");
        obj.put("url","www.codewithz.com");
        obj.put("author","Zartab Nakhwa");

        String jsonString=obj.toString(5); // 5 in the toString() is to specify the indentation
        System.out.println(jsonString);
        }
}

Output:

{
     "author": "Zartab Nakhwa",
     "name": "Code with Z",
     "url": "www.codewithz.com"
}

Lets add an array of phoneNumbers only containing values into this JSONObject using put method and passing a List of String.

List numbers=new ArrayList<>();
        numbers.add("12345");
        numbers.add("54321");
        numbers.add("098765");
        numbers.add("999888");


        obj.put("phone",numbers);

// After adding this code the output looks like.

Output:

{
     "phone": [
          "12345",
          "54321",
          "098765",
          "999888"
     ],
     "author": "Zartab Nakhwa",
     "name": "Code with Z",
     "url": "www.codewithz.com"
}

Lets add an array of JSONObject to store the office and hq address

Also observe how I have used the JSON String inside the constructor of JSONObject

List addresses=new ArrayList<>();


        JSONObject address1=new JSONObject("{type:office,area:Mumbai,sector:20,plot:53}");
        JSONObject address2=new JSONObject("{type:hq,area:Navi Mumbai,sector:10,plot:35}");


        addresses.add(address1);
        addresses.add(address2);
        obj.put("address",addresses);

// After adding this code the output looks like.


Output:

{
     "address": [
          {
               "area": "Mumbai",
               "plot": 53,
               "type": "office",
               "sector": 20
          },
          {
               "area": "Navi Mumbai",
               "plot": 35,
               "type": "hq",
               "sector": 10
          }
     ],
     "phone": [
          "12345",
          "54321",
          "098765",
          "999888"
     ],
     "author": "Zartab Nakhwa",
     "name": "Code with Z",
     "url": "www.codewithz.com"
}

Lets add an JSONObject by name into existing object to display the serverDetails

JSONObject serverHosting=new JSONObject("{hostingStatus:active,serverOS:Linux}");
    obj.put("serverDetails",serverHosting);

// After adding this code the output looks like.

Output:

{
     "address": [
          {
               "area": "Mumbai",
               "plot": 53,      
               "type": "office",
               "sector": 20
          },
          {
               "area": "Navi Mumbai",
               "plot": 35,
               "type": "hq",
               "sector": 10
          }
     ],
     "serverDetails": {
          "hostingStatus": "active",
          "serverOS": "Linux"
     },
     "phone": [
          "12345",
          "54321",
          "098765",
          "999888"
     ],
     "author": "Zartab Nakhwa",
     "name": "Code with Z",
     "url": "www.codewithz.com"
}

Now lets make a JSONObject using a Map object

Here we will first create an Map object and insert the data into using usual procedure, later we will pass the object of Map to JSONObject constructor as an argument which will convert the content of Map into a JSONObject.

import org.json.*;
import java.util.*;


public class JSONFromMap
{
    public static void main(String[] args)
    {
        Map map=new TreeMap<>();


        map.put("name","Virat Kohli");
        map.put("age","30");
        map.put("gender","male");
        map.put("country","India");
        
        JSONObject obj=new JSONObject(map);


        String jsonString=obj.toString(5);
        System.out.println(jsonString);
    }
    
}

Output:

{
     "name": "Virat Kohli",
     "country": "India",
     "gender": "male",
     "age": "30"
}

Related Posts

language_img
Java

Camel Casing Rule

Jul 7, 2020

Table Of Contents

;