1

We have the following json string:

jsonUserDataRecs=[{"G_ID1":1,"G_ID2":2,"G_ID3":3,"NAME":"g1"},{"G_ID1":4,"G_ID2":5,"G_ID3":5,"NAME":"g2"}]

Would like to convert it into the following data type:

ArrayList<USER_DATA_REC> userDataRecs;

We tried the following, but did not get any results:

userDataRecs = gson.fromJson(jsonUserDataRecs, new TypeToken<ArrayList<USER_DATA_REC>>() {}.getType());

When we run : userDataRecs.get(0).getNAME(), we did not get any results.

Any help will be greatly appreciated.

Thank you.

2

1 Answer 1

2

First make a POJO class:

public class UserDataRecord {
    public int G_ID1;
    public int G_ID2;
    public int G_ID3;
    public String NAME;
    public String getNAME() {
        return NAME;
    }
}

Next use gson to deserialize your json string like so:

UserDataRecord[] userDataRecords = gson.fromJson(jsonUserDataRecs, UserDataRecord[].class);

Now you have an array of the deserialized user data records. You can manually convert this to a list if you need to, or just iterate it using a normal for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

if i have multiple class inside parent class then what can i do to get the value of child classes?

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.