Monday 20 November 2017

Parse and write JSON data in Java with Gson

Here i am showing you full examples of using following Gson streaming APIs to read and write JSON data.

As a pre-requisite, you are required to download the gson-2.1.jar




Create A GetJson.java class as bellow.


public class GetJson {
   public static void main(String args[]){
   GetJson  obj= new GetJson(); 
   obj.getJsonData();
   } 
   public void getJsonData(){
   
   //[{amount":9283.41,"balanceAfterTransaction":9283.41,"bank":"Bank_0160021000998","category":"OPENING_BALANCE","dateTime":"01/05/2017","description":"OPENING BALANCE","remark":null,"transactionNumber":null,"type":"CREDIT","valueDate":null},{"amount":100000.0,"balanceAfterTransaction":109283.41,"bank":"Bank_01698210009909","category":"OTHER","dateTime":"01/05/2017","description":" BY INST 51 : CTO12- 1 DAY LAT","remark":null,"transactionNumber":"KZT2","type":"CREDIT","valueDate":null}]    
   String inputLine = "[{amount\":9283.41,\"balanceAfterTransaction\":9283.41,\"bank\":\"Bank_0160021000998\",\"category\":\"OPENING_BALANCE\",\"dateTime\":\"01/05/2017\",\"description\":\"OPENING BALANCE\",\"remark\":null,\"transactionNumber\":\"trx009\",\"type\":\"CREDIT\",\"valueDate\":null},{\"amount\":100000.0,\"balanceAfterTransaction\":109283.41,\"bank\":\"Bank_01698210009909\",\"category\":\"OTHER\",\"dateTime\":\"01/05/2017\",\"description\":\" BY INST 51 : CTO12- 1 DAY LAT\",\"remark\":null,\"transactionNumber\":\"KZT2\",\"type\":\"CREDIT\",\"valueDate\":null}]";    
   String jsonData = "{\"bankTransactionList\":"+inputLine+"}";
   int sizeList =0;
    String amount =null;
String balanceAfterTransaction =null;
String bank =null;
String category = null;
String dateTime = null;
String description = null;
String remark = null;
String transactionNumber = null;
String type = null;
String valueDate = null;
   try{
   Gson gson = new Gson();
   BankArrayList myData = gson.fromJson(jsonData, BankArrayList.class);
   System.out.println("myDataList size >> "+myData.bankTransactionList.size());
   sizeList = myData.bankTransactionList.size();
   for(int i=0;i<sizeList;i++){
   System.out.println("Amount >> "+ myData.bankTransactionList.get(i).amount);
   System.out.println("balanceAfterTransaction >> "+ myData.bankTransactionList.get(i).balanceAfterTransaction);
   System.out.println("bank >> "+ myData.bankTransactionList.get(i).bank);
   System.out.println("dateTime >> "+ myData.bankTransactionList.get(i).dateTime);
   System.out.println("description >> "+ myData.bankTransactionList.get(i).description);
   System.out.println("transactionNumber >> "+ myData.bankTransactionList.get(i).transactionNumber);
   System.out.println("type >> "+ myData.bankTransactionList.get(i).type);
   System.out.println("valueDate >> "+ myData.bankTransactionList.get(i).valueDate);
   System.out.println("--------------------------------------------------------");
   
   }
   
   }catch(Exception ex){
  ex.printStackTrace(); 
   }
   }

}


Create A Bank.java class as bellow.

public class Bank {
public String amount;
public String balanceAfterTransaction;
public String bank;
public String category;
public String dateTime;
public String description;
public String remark;
public String transactionNumber;
public String type;
public String valueDate;
}


Create A BankArrayList.java class as bellow.

public class BankArrayList {
public ArrayList<Bank> bankTransactionList = new ArrayList <Bank>();

}

After run GetJson.java file, Result will be display as bellow image






No comments:

Post a Comment