Make sure to subscribe to our newsletter and be the first to know the news.
Make sure to subscribe to our newsletter and be the first to know the news.
There are multiple ways if creating q JSON Object, using various constructor of JSONObject
JSONObject() -- creates a blank JSON Object and can be filled up using put() methods
JSONObject(String jsonString) -- creates a JSONObject based upon the provided String representation of JSON.
JSONObject(java.util.Map map) -- creates a JSONObject based upon the provided map object.
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"
}
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"
}
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"
}
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"
}