Friday 3 July 2015

Introduction to json data in Android/Java


Introduction:
        JSON is an XML alternative. It is the communication and data exchange protocol, created mainly to use in JavaScript (JSON = JavaScript Object Notation), however it is also widely used in Java or Android apps to exchange data over the internet. Basic JSON message (JSONObject) consists of  key-value pairs, like this one:
eg:1
{ 
     "userid":238,
     "username":"Pavan",
     "password":"password",
     "isgoodprogrammer":1
} 
JSON is fat-free, compared to XML, because there is significant difference in amount of metadata attached to message. Just compare XML to JSON message. This XML has exactly the same user data as JSON in the example above(more example clic here)
eg:2 
<user>
        <userid>238</userid> 
        <username>Pavan</username>
        <password>password</password>
        <isgoodprogrammer>1</isgoodprogrammer>
</user> 

Android/Java

         Android has built-in JSON utils,and for java, there is jar to include to application(for example json-org.jar).Add jar to Build Path.
Using JSON in Java code is the same for both Android and Java.

Simple JSON Object
         JSONObject is the simpliest key-value pair structure in JSON. Creating JSONObject like in eg:1 is done by this code:
JSONObject user = new JSONObject();
user.put("userid", 238);
user.put("username", "Pavan");
user.put("password", "password");
user.put("isgoodprogrammer", 1); 
Reading value from JSONObject is done by:
int userId = user.getInt("userid");
String userName = user.optString("userName");
getString() – returns String containing firstName or throws JSONException
if there was no object with key = “firstName”

optString() – returns String containing firstName or empty String (“”)
if there was no object with key = “firstName”

Simple JSON Array of objects:
         Another structure is JSONArray. This is a set of JSONObjects (note that JSONArray is also a JSONobject). 
here is sample of Java code how to create JSONArray of users:
JSONObject user = new JSONObject();
user.put("userid", 238);
user.put("username", "Pavan");
user.put("password", "password");
user.put("isgoodprogrammer", 1);

JSONObject user2 = new JSONObject();
user2.put("userid",282);
user2.put("username", "Bob");
user2.put("password", "security");
user2.put("isgoodprogrammer", 1);

JSONArray clients = new JSONArray();
clients.put(user);
clients.put(user2);
and the JSON message produced by this code is: 
[
 {
       "userid":238,
       "username":"Pavan",
       "password":"password",
       "isgoodprogrammer":1
 },
 {
       "userid":282,
       "username":"Bob",
       "password":"security",
       "isgoodprogrammer":1
 }
]  
(note that keys are not ordered as in Java code. Order does not matter)
here order is not important. 
Reading JSONObject from JSONArray is done by:
for (int i = 0; i<clients.length();i++) {
 JSONObject usr = clients.getJSONObject(i);
}
JSON features:
1. JSONObjects and Arrays can be nested as many times as needed. JSONArray can be added to JSONObject as element with its id by:
JSONObject.put("key", new JSONArray); 
2. JSONObject can be easily serialized to String in order to write to file or send over the Internet. The library also reads JSONObject from String (remember to handle JSONException, that is thrown when String input is not well formatted). See sample here:
JSONObject user = new JSONObject();
user.put("userid", 238);
String JsonString = user.toString();
JSONObject object = new JSONObject(JsonString);
3. There are utils that enables to convert Java objects, like Hashtables to JSON just by one line of code(see  GSON)


No comments: