Wednesday, 24 May 2017

Creating JSON String Using CLASSES

In Json String basically we have two types of Variables
1. JSONObject – {}
2. JSONArray  - []

JSONObject allows to store values in the form of key,value pair.
JSONArray allows to store values in the form of array.


Insert data into JSONObject use put(key,value);
Insert data into JSONArray use put(value);

put(key,value) is a method to store data into JSONObject.
put(key) is a method to store data into JSONArray.


1. Construct JsonObject
// creating JsonObject
JSONObject jsonObject = new JSONObject();

// inserting value into JsonObject syntax
jsonObject.put(key, value);                        

// inserting name into JsonObject
jsonObject.put("name", "venkat");

// inserting phno into JsonObject
jsonObject.put("phno", 1234567890);

// inserting city into JsonObject
jsonObject.put("city", "Hyd");

// showing JsonObject
Log.e("jsonObject", jsonObject.toString());

Output
************************************

   "name":"venkat",
   "phno":1234567890,
   "city":"Hyd"
}
************************************               

2. JsonObject inside another JsonObject            
// creating json objects
JSONObject outerObject = new JSONObject();
JSONObject innerObject = new JSONObject();
                            
// inserting values into inner object
innerObject.put("name", "venkat");
innerObject.put("phno", 1234567890);
innerObject.put("city", "Hyd");
                            
// inserting inner object into outer object
outerObject.put("object", innerObject);
                            
// showing outer object
Log.e("outerObject", outerObject.toString());
               
Output
************************************

   "object":{ 
      "name":"venkat",
      "phno":1234567890,
      "city":"Hyd"
   }
}   
************************************

3. Construct JsonArray         
// creating json array
JSONArray jsonArray = new JSONArray();
jsonArray.put("venkat");
jsonArray.put("Hyd");
jsonArray.put("0123456789");
                            
// showing jsonarray
Log.e("jsonarray", jsonArray.toString());

Output
************************************

   "venkat",
   "Hyd",
   "0123456789"
]              
************************************
             
4. JsonArray inside JsonArray
// creating json arrays
JSONArray innerArray = new JSONArray();
JSONArray outerArray = new JSONArray();

// inserting values into inner array
innerArray.put("venkat");
innerArray.put("Hyd");
innerArray.put("0123456789");

// inserting inner array into outer array
outerArray.put(innerArray);

// showing outer array
Log.e("outer array", outerArray.toString());

Output
************************************

   [ 
      "venkat",
      "Hyd",
      "0123456789"
   ]
]             
************************************

5. JsonObject inside JsonArray
// creating json object
JSONObject jsonObject = new JSONObject();
// creating json array
JSONArray jsonArray = new JSONArray();

// inserting values into json array
jsonArray.put("venkat");
jsonArray.put("Hyd");
jsonArray.put("0123456789");

// inserting json array into json object
jsonObject.put("array", jsonArray);

// showing jsonObject
Log.e("jsonobject", jsonObject.toString());

Output
************************************

   "array":[ 
      "venkat",
      "Hyd",
      "0123456789"
   ]
}              
************************************
             
6. JsonArray inside JsonObject
// creating json array
JSONArray jsonArray = new JSONArray();
// creating json object
JSONObject jsonObject = new JSONObject();

// inserting values into json object
jsonObject.put("name", "venkat");
jsonObject.put("phno", "0123456789");
jsonObject.put("city", "Hyd");

// inserting json object into json array
jsonArray.put(jsonObject);

// showing jsonarray
Log.e("jsonarray", jsonArray.toString());

Output
************************************
 [ 
   { 
      "name":"venkat",
      "phno":"0123456789",
      "city":"Hyd"
   }
]             
************************************

No comments:

Post a Comment