GSON does not fill out the list of objects

I tried several solutions, and the result of parsing JSON with GSON is always wrong.

I have the following JSON:

{
    "account_list": [
        {
            "1": {
                "id": 1,
                "name": "test1",
                "expiry_date": ""
            },
            "2": {
                "id": 2,
                "name": "test2",
                "expiry_date": ""
            }
        }
    ]
}

In my Java project, I have the following structures:

public class Account{

    private int id;
    private String name;
    private String expiry_date;

    public Account()
    {
        // Empty constructor
    }

    public Account(int id, String name, String expiry_date)
    {    
        this.id = id;
        this.name = name;
        this.expiry_date = expiry_date;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getExpiryDate() {
        return expiry_date;
    } 
}

and

public class AccountList{
    private List <Account> account_list;

    public void setAccountList(List <Account> account_list) {
        this.account_list = account_list;
    }

    public List <Account> getAccountList() {
        return account_list;
    }
}

And what am I doing for deserialization:

Data.account_list = new Gson().fromJson(content, AccountList.class);

In the end, I get a List with only one element and with the wrong values. Can you tell me what I'm doing wrong?

Thank.

+3
source share
1 answer

javabean JSON ( ). account_list JSON , , , Account, -, . Gson Account.

javabean, JSON :

{
    "account_list": [
        {"id": 1, "name": "test1", "expiry_date": ""},
        {"id": 2, "name": "test2", "expiry_date": ""}
    ]
}

JSON, Javabean. JSON , . List<Map<Integer, Account>> List<Account> AccountList. List<Account>, Gson.

+9

Source: https://habr.com/ru/post/1767539/


All Articles