0

I have to map this JSONObject into a Java object. This is my Json:

{"WALLET":{  
            "ID":"1234",
            "BAL":"20.000",
            "NAME":"Filomena",
            "EMAIL":"[email protected]",
            "DOCS":[  
                {  
                    "ID":"001",
                    "S":"0",
                    "TYPE":"CardId",
                    "VD":"2019"
                }
            ],
            "IBANS":[  
                {  
                    "ID":"001",
                    "S":"1",
                    "DATA":"iban",
                    "SWIFT":"swiftCode",
                    "HOLDER":"holder"
                }
            ],
            "STATUS":"string",
            "BLOCKED":"1",
            "SDDMANDATES":[  
                {  
                    "ID":"sddMandateId",
                    "S":"status",
                    "DATA":"iban",
                    "SWIFT":"swiftCode"
                }
            ],
            "LWID":"string",
            "CARDS":[  
                {  
                    "ID":"string",
                    "EXTRA":{  
                        "IS3DS":"string",
                        "CTRY":"string",
                        "AUTH":"string",
                        "NUM":"string",
                        "EXP":"string",
                        "TYP":"string"
                    }
                }
            ],
            "FirstName":"string",
            "LastName":"string",
            "CompanyName":"string",
            "CompanyDescription":"string",
            "CompanyWebsite":"string"
        }
}

This is my Java class:

public class Wallet {

    private String id;
    private String bal;
    private String name;
    private String email;
    private List<Doc> docs;
    private List<Iban> ibans;
    private String status;
    private String blocked;
    private List<SddMandate> sddMandates ;
    private String lwid;
    private List<Card> cards;
    private String firstName;
    private String lastname;
    private String companyName;
    private String companyDescription;
    private String companyWebSite;



    public Wallet(){

    }




    public Wallet(String id, String bal, String name, String email, List<Doc> docs, List<Iban> ibans, String status,
            String blocked, List<SddMandate> sddMandates, String lwid, List<Card> cards, String firstName,
            String lastname, String companyName, String companyDescription, String companyWebSite) {
        super();
        this.id = id;
        this.bal = bal;
        this.name = name;
        this.email = email;
        this.docs = docs;
        this.ibans = ibans;
        this.status = status;
        this.blocked = blocked;
        this.sddMandates = sddMandates;
        this.lwid = lwid;
        this.cards = cards;
        this.firstName = firstName;
        this.lastname = lastname;
        this.companyName = companyName;
        this.companyDescription = companyDescription;
        this.companyWebSite = companyWebSite;
    }




    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getBal() {
        return bal;
    }
    public void setBal(String bal) {
        this.bal = bal;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public List<Doc> getDocs() {
        return docs;
    }
    public void setDocs(List<Doc> docs) {
        this.docs = docs;
    }
    public List<Iban> getIbans() {
        return ibans;
    }
    public void setIbans(List<Iban> ibans) {
        this.ibans = ibans;
    }



    public String getStatus() {
        return status;
    }



    public void setStatus(String status) {
        this.status = status;
    }



    public String getBlocked() {
        return blocked;
    }



    public void setBlocked(String blocked) {
        this.blocked = blocked;
    }



    public List<SddMandate> getSddMandates() {
        return sddMandates;
    }



    public void setSddMandates(List<SddMandate> sddMandates) {
        this.sddMandates = sddMandates;
    }



    public String getLwid() {
        return lwid;
    }



    public void setLwid(String lwid) {
        this.lwid = lwid;
    }



    public List<Card> getCards() {
        return cards;
    }



    public void setCards(List<Card> cards) {
        this.cards = cards;
    }



    public String getFirstName() {
        return firstName;
    }



    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }



    public String getLastname() {
        return lastname;
    }



    public void setLastname(String lastname) {
        this.lastname = lastname;
    }



    public String getCompanyName() {
        return companyName;
    }



    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }



    public String getCompanyDescription() {
        return companyDescription;
    }



    public void setCompanyDescription(String companyDescription) {
        this.companyDescription = companyDescription;
    }



    public String getCompanyWebSite() {
        return companyWebSite;
    }



    public void setCompanyWebSite(String companyWebSite) {
        this.companyWebSite = companyWebSite;   
}

Now i'm trying to map the object with gson library.

Wallet walletDetails=gson.fromJson(rispostaGetWalletDetails.toString(), Wallet.class);
   System.out.println("Balance: "+walletDetails.getBal());

Now when i try to call method on the object i have always null and not the real value. How i can do?

3
  • Have you tried to output ? System.out.println(walletDetails); Commented May 9, 2017 at 11:01
  • yes, there is the string Commented May 9, 2017 at 11:01
  • Isn't gson case sensitive? Have you tried to change your json to lowercase? Commented May 9, 2017 at 11:02

3 Answers 3

5

You have a wrong root level. Probably, you need to need to get one level down

JSONObject yourObject = json.get("WALLET");
Wallet walletDetails = gson.fromJson(yourObject.toString(), Wallet.class);
Sign up to request clarification or add additional context in comments.

1 Comment

Great intuition, I missed that! Anyway, I think that it still won't handle the field name mismatch between Java and JSON. See my answer for clarification.
1

To have Gson handle the correct field name mapping while deserializing, you have to register a FieldNamingStrategy like this (using Java 8):

Gson gson = new GsonBuilder()
            .setFieldNamingStrategy(field -> field.getName().toUpperCase())
            .create();

The strategy will convert each Java field name to match those in your JSON. This will cover almost all your fields except for those upper-camel-cased in the JSON response, such as "LastName", "CompanyName", etc. In order to map those too, your FieldNamingStrategy will have to become a little bit smarter, like:

field -> {
    String fname = field.getName();
    return "firstName".equals(fname) || "companyName".equals(fname) /*etc...*/ ? capitalize(fname) : fname.toUpperCase();
}

and so on, I think you got the idea. The capitalize() method you can find in libraries like Apache Commons Lang or write your own, it's just for examplification here.

2 Comments

maybe i will receive lowercase json, for now it works with the answer of Dezigo. Thank you anyway.
You're welcome, but I don't understand how it can work for you using a plain Gson instance... I tried a quick test using your JSON and class, and it just won't map the fields with default Gson settings, am I missing something? Good for you if it works, I just don't get how :)
0

Your object variable name doesn't match the json attribute name. "EMAIL" in json should have same EMAIL in object. To overcome this, you could mention @JsonProperty before your attribute declaraction.

for eg:

@JsonProperty("EMAIL") private String email;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.