Thursday, 11 September 2014

JSON with json.simple Library

Maven Dependency:

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
  </dependency>


Student Pojo:

package com.ram.json.simple.pojo;

import java.util.ArrayList;
import java.util.List;

public class Student {
   
    public String name;
    public String age;
    public List<String> list = new ArrayList<String>();
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
   
   

}


Convert Java To JSON:

package com.ram.json.simple;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONObject;

import com.ram.jackson.pojo.Student;

public class JavaToJson {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        Student student = new Student();
        student.setName("Ramakrishna");
        student.setAge("26");
        List<String> list = new ArrayList<String>();
        list.add("100");
        list.add("200");
        list.add("300");
        student.setList(list);
       
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("Name", student.getName());
        jsonObject.put("Age", student.getAge());
        jsonObject.put("list", student.getList());
   
        try {
   
            FileWriter file = new FileWriter("/home/biton/ramakrishna/delete/json/student.json");
            file.write(jsonObject.toJSONString());
            file.flush();
            file.close();
   
        } catch (IOException e) {
            e.printStackTrace();
        }
   
        System.out.print(jsonObject);
   
      }
   
    }


Convert JSON To Java:

package com.ram.json.simple;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


public class JsonToJava {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
       
        JSONParser parser = new JSONParser();
       
        try {
   
            Object object = parser.parse(new FileReader("/home/biton/ramakrishna/delete/json/student.json"));
   
            JSONObject jsonObject = (JSONObject) object;
           
            System.out.println(jsonObject);
   
            String name = (String) jsonObject.get("Name");
            System.out.println(name);
   
            String age = (String) jsonObject.get("Age");
            System.out.println(age);
           
            List<String> list = (List<String>) jsonObject.get("list");
            System.out.println(list);
   
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
       
      }

}



JSON with gson Library

Maven Dependency:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>1.7.1</version>
  </dependency>


Student Pojo:

package com.ram.gson.pojo;

import java.util.ArrayList;
import java.util.List;

public class Student {
   
    public String name;
    public String age;
    public List<String> list = new ArrayList<String>();
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }   
}


Convert Java To JSON:

package com.ram.gson;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.ram.jackson.pojo.Student;

public class JavaToJson {

    public static void main(String[] args) {

        Student student = new Student();
        student.setName("Ramakrishna");
        student.setAge("26");
        List<String> list = new ArrayList<String>();
        list.add("100");
        list.add("200");
        list.add("300");
        student.setList(list);
       
        Gson gson = new Gson();
   
        String json = gson.toJson(student);
   
        try {

            FileWriter writer = new FileWriter("/home/ramakrishna/delete/json/student.json");
            writer.write(json);
            writer.close();
   
        } catch (IOException e) {
            e.printStackTrace();
        }
   
        System.out.println(json);
      }
   
    }



Convert JSON To Java:

package com.ram.gson;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.google.gson.Gson;
import com.ram.gson.pojo.Student;

public class JsonToJava {

    public static void main(String[] args) {
       
        Gson gson = new Gson();
       
        try {
   
            BufferedReader br = new BufferedReader(
                new FileReader("/home/ramakrishna/delete/json/student.json"));
   
            Student student = gson.fromJson(br, Student.class);
   
            System.out.println("Name:"+student.getName());
            System.out.println("Age:"+student.getAge());
            System.out.println("Values:"+student.getList());
   
        } catch (IOException e) {
            e.printStackTrace();
        }
       
      }

}




JSON with Jackson Library

Maven Dependency:

<!-- For Jackson -->
  <repositories>
    <repository>
        <id>codehaus</id>
        <url>http://repository.codehaus.org/org/codehaus</url>
    </repository>
  </repositories>

<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.5</version>
</dependency>

Studen Pojo:

package com.ram.jackson.pojo;

import java.util.ArrayList;
import java.util.List;

public class Student {
   
    public String name;
    public String age;
    public List<String> list = new ArrayList<String>();
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
}
 


Convert Java To JSON:

package com.ram.jackson;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.ram.jackson.pojo.Student;

public class JavaToJson {

    public static void main(String[] args) {

        Student student = new Student();
        student.setName("Ramakrishna");
        student.setAge("26");
        List<String> list = new ArrayList<String>();
        list.add("100");
        list.add("200");
        list.add("300");
        student.setList(list);
       
        ObjectMapper mapper = new ObjectMapper();
   
        try {
   
            mapper.writeValue(new File("/home/ramakrishna/delete/json/student.json"), student);
   
            System.out.println(mapper.writeValueAsString(student));
   
        } catch (JsonGenerationException e) {
   
            e.printStackTrace();
   
        } catch (JsonMappingException e) {
   
            e.printStackTrace();
   
        } catch (IOException e) {
   
            e.printStackTrace();
   
        }
   
      }
   
    }

Convert JSON To Java:

package com.ram.jackson;

import java.io.File;
import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.ram.jackson.pojo.Student;

public class JsonToJava {

    public static void main(String[] args) {
       
        ObjectMapper mapper = new ObjectMapper();
       
        try {
   
            Student student = mapper.readValue(new File("/home/ramakrishna/delete/json/student.json"), Student.class);
   
            System.out.println("Name:"+student.getName());
            System.out.println("Age:"+student.getAge());
            System.out.println("Values:"+student.getList());
           
   
        } catch (JsonGenerationException e) {
   
            e.printStackTrace();
   
        } catch (JsonMappingException e) {
   
            e.printStackTrace();
   
        } catch (IOException e) {
   
            e.printStackTrace();
   
        }
   
      }
}





JSON Introduction

JSON Introduction

JSON (JavaScript Object Notation) is a lightweight data-interchange format.

Format of JSON is  Key with Value pair 

It is easy for humans to read and write. It is easy for machines to parse and generate.

JSON format is used for serializing & transmitting structured data over network connection.

Web Services and API'S use JSON format to provide public data.

 JSON is language independent like XML

Java libraries to process JSON data, which are Jackson, Google Gson and JSON.simple