Thursday 10 August 2017

JSON



JavaScript Object Notation(JSON) is a lightweight text-based open standard designed for human-readable data interchange.
It is primarily used to transmit data between a server and web applications.
Web services and APIs use JSON format to provide public data.

The more lightweight JSON (Javascript object notation) has become a popular alternative to XML for various reasons.

1. Less verbose- XML uses more words than necessary
2. JSON is faster- Parsing XML software is slow and cumbersome. Many of these DOM manipulation libraries can lead to your applications using large amounts of memory due to the verbosity and cost of parsing large XML files.
3. This XML structure is not intuitive, making it hard to represent in code.
    JSON’s structure is intuitive, making it easy to read and map directly to domain  objects in whatever programming language is being used.


The more lightweight JSON (Javascript object notation) has become a popular alternative to XML for various reasons.
 

JSON and XML are human readable formats and are language independent.


JSON

{
   "company": Volkswagen,
   "name": "Vento",
   "price": 800000
}

XML


   Volkswagen
Vento 800000

JSON encoding/decoding

JSONJava
stringjava.lang.String
numberjava.lang.Number
true|falsejava.lang.Boolean
nullnull
arrayjava.util.List
objectjava.util.Map
  
JSON encoding
import org.json.simple.JSONObject;

class JsonEncodeDemo {

   public static void main(String[] args){
 
      JSONObject obj = new JSONObject();

      obj.put("name","foo");
      obj.put("num",new Integer(100));
      obj.put("balance",new Double(1000.21));
      obj.put("is_vip",new Boolean(true));

      StringWriter out = new StringWriter();
      obj.writeJSONString(out);
      
      String jsonText = out.toString();
      System.out.print(jsonText);
   }
}
On compiling and executing the above program, the following result is generated −
{"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"}

JSON Decoding or parsing
{
   "pageInfo": {
         "pageName": "abc",
         "pagePic": "http://example.com/content.jpg"
    }
    "posts": [
         {
              "post_id": "123456789012_123456789012",
              "actor_id": "1234567890",
              "picOfPersonWhoPosted": "http://example.com/photo.jpg",
              "nameOfPersonWhoPosted": "Jane Doe",
              "message": "Sounds cool. Can't wait to see it!",
              "likesCount": "2",
              "comments": [],
              "timeOfPost": "1234567890"
         }
    ]
}

 
import org.json.*;


JSONObject obj = new JSONObject(" .... ");
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
}

To covert JAVA object to JSON

  • toJson() – Convert Java object to JSON format
  • fromJson() – Convert JSON into Java object
`
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;

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

    Gson gson = new Gson();

    try {

        BufferedReader br = new BufferedReader(
            new FileReader("c:\\file.json"));

        //convert the json string back to object
        DataObject obj = gson.fromJson(br, DataObject.class);

        System.out.println(obj);

    } catch (IOException e) {
        e.printStackTrace();
    }

    }
}

 

No comments:

Post a Comment