Search code examples
javagsonpojo

Mapping JSON to Java Class


I want to map JSON output to a Java class It is a response to a REST API request. I have attached some of the JSON that I am having problems with. It is a smaller section of a larger file. The issue is it is not a standard property/value pair such as

"color" : "red"

It looks like this:

{"transactions" : {
         "transaction_id_005" : {
            "complete" : false,
            "confirmed" : false,
            "event_transaction_id" : "transaction_id_005_11223344_1714400505026_SWupdate-000002",
            "event_type" : "software_update",
            "expiration_mark" : false}
   }
}

This is the code I have.


Transaction trans = new Gson().fromJson(transstring, Transaction.class);
        }
        
        catch (Exception e) {
            System.out.println(e.getMessage());
        }

and the classes:

public class Transaction {
    
    public String transactions = "";
    public Embedded embedded = null;
    public String getTransactions() {
        return transactions;
    }
    public void setTransactions(String transactions) {
        this.transactions = transactions;
    }
    public Embedded getEmbedded() {
        return embedded;
    }
    public void setEmbedded(Embedded embedded) {
        this.embedded = embedded;
    }
}

class Embedded {
    public String complete ="";
    public String confirmed ="";
    public String event_transaction_id = "";
    public String event_type = "";
    public String expiration_mark = "";
}

It throws this exception: gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 20 path $.transactions

Which I have found means it is not expecting that opening { in the first line after "transactions" :


Solution

  • A representation of your json is a Class Transaction with a property transactions that is a Map<String,Embedded>

    import java.util.Map;
    
    import com.google.gson.Gson;
    
    public class Transaction {
        
        public Map<String,Embedded> transactions; 
        public Map<String,Embedded> getTransactions() {
            return transactions;
        }
        public void setTransactions(Map<String,Embedded> transactions) {
            this.transactions = transactions;
        }
    
    
        public static void main(String[] argv) {
            Transaction trans = new Gson().fromJson("{\"transactions\" : {\r\n"
                    + "         \"transaction_id_005\" : {\r\n"
                    + "            \"complete\" : false,\r\n"
                    + "            \"confirmed\" : false,\r\n"
                    + "            \"event_transaction_id\" : \"transaction_id_005_11223344_1714400505026_SWupdate-000002\",\r\n"
                    + "            \"event_type\" : \"software_update\",\r\n"
                    + "            \"expiration_mark\" : false}\r\n"
                    + "   }\r\n"
                    + "}", Transaction.class);
    
            System.out.println(trans);
        
        }
    
    }
    
    class Embedded {
        public String complete ="";
        public String confirmed ="";
        public String event_transaction_id = "";
        public String event_type = "";
        public String expiration_mark = "";
    }