How to Create JSON in Java ?

we can easily create JSON in java Using org.json.simple library .so build this jar into your application .

  • Create JSONObject  :

public class JSON{
 public static void main(String[] args) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Name", "Anand");
        jsonObject.put("Age", new Integer(23));
        jsonObject.put("Married",new Boolean(false));


     System.out.println(jsonObject); 
/*

{"Married":false,"Age":23,"Name":"Anand"}

*/

}

  • Create JSONArray : 
 
public class JSON{
 public static void main(String[] args) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Name", "Anand");
        jsonObject.put("Age", new Integer(23));
        jsonObject.put("Married",new Boolean(false));

     JSONArray jsonarray = new JSONArray();
        jsonarray.add(jsonObject);//


     System.out.println(jsonarray); 
/*
 [{"Married":false,"Age":23,"Name":"Anand"}]
*/

}


 

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map