0

I have some JSON objects and I need to changed them as JAVA classes and assign the given value.

{
 "Summary":{
  "AccountSummary":{
   "Account_number": "324d",
   "Account_name": "John"
  },
  "Transaction":[
   {
    "Date": "2021-08-21",
    "Amount": "20,000"
   },
   {
    "Date": "2021-08-23",
    "Amount": "5,000"
   }
  ]
 }
}

These are the current coding I did,

//The account summary class with assigned value
public class AccountSummary{
    @JsonProperty("Account_number")
    public String account_number = "324d";
    @JsonProperty("Account_name")
    public String account_name = "John";
}

//Transaction class. I want to know how I can assign values
public class Transaction{
    @JsonProperty("Date")
    public String date;
    @JsonProperty("Amount")
    public String amount;
}

// Summary class
public class Summary{
    @JsonProperty("AccountSummary")
    public AccountSummary accountSummary;
    @JsonProperty("Transaction")
    public List<Transaction> transaction;
}

As I have assigned values for AccountSummary, I need to assign values for Transaction class also. But As if its a list I don't know how to assign. Please help.

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
Deepika
  • 737
  • 7
  • 23
  • you can use GSON it will directly convert JSON to POJO https://stackoverflow.com/questions/31073624/how-to-convert-json-objects-to-pojo-from-gson – Ganesh Gudghe Aug 27 '21 at 10:25
  • why dont you use this tool directly? https://www.jsonschema2pojo.org/ – tsamridh86 Aug 27 '21 at 10:31
  • Why do you want to statically assign values in the code instead of parsing the Json file with Jackson and let it assign the values instead? If you really want to do that, you can use `List.of()` method to statically assign the values to a list but again, it looks weird. – Matteo NNZ Aug 27 '21 at 10:39
  • @MatteoNNZ I am unable to tell the reason because it is a client project. I really need to assign values manually. – Deepika Aug 27 '21 at 10:55
  • @SamridhTuladhar that website is not working to me. When I select preview it is showing an empty popup – Deepika Aug 27 '21 at 10:59

2 Answers2

0

You can do something like this,
First, create a maven project which has the below dependencies,
1. jackson-core
2. jackson-databind
3. jackson-annotations
make sure to have all three dependencies in the same version.

Then create separate model classes for your POJO objects

public class AccountSummary{
    @JsonProperty("Account_number") 
    public String account_number;
    @JsonProperty("Account_name") 
    public String account_name;
}

public class Transaction{
    @JsonProperty("Date") 
    public String date;
    @JsonProperty("Amount") 
    public String amount;
}

public class Summary{
    @JsonProperty("AccountSummary") 
    public AccountSummary accountSummary;
    @JsonProperty("Transaction") 
    public List<Transaction> transaction;
}

public class Root{
    @JsonProperty("Summary")
    public Summary summary;
}

Then create a Main.java class and implement the below code.

public class Main {

    public static void main(String[] args) {
        try {
            ObjectMapper om = new ObjectMapper();
            Root root  = om.readValue(new File("your.value.json"),Root.class);
            System.out.println(root);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

now when you execute you can see all the values in the json file are assigned correctly to your models.

Debug mode screenshot
So like this, you can map your json values to your objects.

0

You can try following for setting the value manually

Summary.java

public class Summary {
    public AccountSummary accountSummary;
    public List<Transaction> transaction;

    public AccountSummary getAccountSummary() {
        return accountSummary;
    }

    public void setAccountSummary(AccountSummary accountSummary) {
        this.accountSummary = accountSummary;
    }

    public List<Transaction> getTransaction() {
        return transaction;
    }

    public void setTransaction(List<Transaction> transaction) {
        this.transaction = transaction;
    }

}

Transaction.java

public class Transaction {
    public String date;
    public String amount;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

}

AccountSummary .java

public class AccountSummary {
    public String account_number;
    public String account_name;

    public String getAccount_number() {
        return account_number;
    }

    public void setAccount_number(String account_number) {
        this.account_number = account_number;
    }

    public String getAccount_name() {
        return account_name;
    }

    public void setAccount_name(String account_name) {
        this.account_name = account_name;
    }

}


final Code:-

    

AccountSummary a = new AccountSummary();
        a.setAccount_name("xyz");
        a.setAccount_number("xyz");

    List<Transaction> transactionList = new ArrayList<Transaction>();
    Transaction t = new Transaction();
    t.setAmount("xyz");
    t.setDate("xyx");
    transactionList.add(t);  // set the multiple object

// here is the final result

    Summary s = new Summary();
    s.setAccountSummary(a);
    s.setTransaction(transactionList);
Ganesh Gudghe
  • 1,327
  • 1
  • 17
  • 41