3

I have the following string:

{"account":{
    "username":"ikevin2222",
    "birthdate":"2017-01-31T09:37:44.000Z",
    "gender":true,
    "emailaddresses": [{
        "emailaddress":"[email protected]",
        "verificationcode":"AAAAAA",
        "isverified":false
    }]
}}

How do I use Google/GSON to convert it to Java POJO?

4
  • Have you tried this : gson.fromJson(jsonString, MyClass.class)? Commented Jan 31, 2017 at 9:43
  • 1
    Write a POJO corresponding to your JSON structure and invoke the method as specified by @Abubakkar in his comment Commented Jan 31, 2017 at 9:48
  • The "account" was causing some confusion, but it's been resolved. Thanks! Commented Jan 31, 2017 at 10:37
  • Possible duplicate of How to parse JSON in Java Commented Feb 4, 2017 at 10:22

1 Answer 1

7

Generate your POJO by using http://www.jsonschema2pojo.org/ or by adding plugin in studio (https://github.com/Hexara/Json2Pojo)

now add dependencies compile 'com.google.code.gson:gson:2.6.2':

convert your json or string to POJO

Gson gson = new Gson();
POJOClass pojo = gson.fromJson(jsonObject.toString(), new TypeToken<POJOClass>() {}.getType());
Sign up to request clarification or add additional context in comments.

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.