Class Array JSON Creation in Java

How to create Class level Array and dynamic generate JSON  using Jackson API


package com.test.rest;

import java.lang.reflect.Array;

import com.bean.Animal;
import com.bean.Employee;
import com.bean.Student;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 *
 * @author anand
 *
 */
public class JsonUtility {
    public ObjectMapper mapper;
    public String jsonString;

    /*
     * This method will Used to Convert ClassObject to JSON  and
     * return as a form of JSON as a String
     */
   
   
    public String ConvetObjectArrayToJSON(Object [] array){
             mapper = new ObjectMapper();
        String jsonArray=null;
        System.out.println(array[i]);
        try{
        for(int i=0;i<array.length;i++){
            System.out.println(array[i].getClass());
           
            jsonString = mapper.writeValueAsString(array[i]);
            jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(array[i]);
            System.out.println(array[i].getClass()+"=="+jsonString);
        }
        }catch(Exception e){
            e.printStackTrace();
        }
       
       
        return jsonArray;
    }
   
    public static void main(String []args){
       
       
        Student student=new Student();
        student.setStudentname("Anand");
        student.setStudentPlace("Bangalore");
        student.setStudentResult("Ok");
       
       
        Employee emp= new Employee();
        emp.setEmployeeName("Antrish");
        emp.setEmployeeLocation("Chennai");
        emp.setEmployeeResult("Capgimini");
       
       
        Animal animal=new Animal();
        animal.setAnimalName("Dog");
        animal.setAnimalPlace("Mumbai");
        animal.setAnimalResult("Eat");
       
       
        Object[] object=new Object[]{student,emp,animal};
        JsonUtility  jsonutility=new JsonUtility();
       
        jsonutility.ConvertObjecttoJSOn(emp);
        jsonutility.ConvetObjectArrayToJSON(object);
          
       
    }
   
}

======================
POJO Classes are

Employee.java

    private String employeeName;
    private String employeeLocation;
    private String employeeResult;

//Setters and Getters


Student.java

    private String studentname;
    private String studentPlace;
    private String studentResult;

//Setters and Getters


    private String animalName;
    private String animalPlace;
    private String animalResult;

// Setters and Getters


=======Output ====


com.bean.Student@17550481
class com.bean.Student
class com.bean.Student=={
  "studentname" : "Anand",
  "studentPlace" : "Bangalore",
  "studentResult" : "Ok"
}
class com.bean.Employee
class com.bean.Employee=={
  "employeeName" : "Antrish",
  "employeeLocation" : "Chennai",
  "employeeResult" : "Capgimini"
}
class com.bean.Animal
class com.bean.Animal=={
  "animalName" : "Dog",
  "animalPlace" : "Mumbai",
  "animalResult" : "Eat"
}







Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map