简体   繁体   中英

Extract objects from this Json string with GSON

i need to get an array of Works for this json string:

  [
        {
            "worktypes": [
                {
                    "works": [
                        {
                            "price": "22,22",
                            "code": "A00011",
                            "name": "name 2"
                        },
                        {
                            "price": "22,22",
                            "code": "A00011",
                            "name": "name 3"
                        },
                        {
                            "price": "22,22",
                            "code": "A00011",
                            "name": "name 4"
                        },
                        {
                            "price": "11",
                            "name": "aa",
                            "code": "aa"
                        },
                        {
                            "price": "12,22",
                            "name": "Nombre",
                            "code": "A00112"
                        },
                        {
                            "price": "22",
                            "name": "asdads",
                            "code": "asdasd"
                        },
                        {
                            "price": "11",
                            "name": "yy",
                            "code": "yy"
                        }
                    ],
                    "name": "Pompas"
                }
            ]
        }
    ]

I try this:

List<Work> works = gson.fromJson(value.toString(), new TypeToken<List<Work>>() {}.getType());

But dont get anyone..whats wrong?

PD:Work is a class with the price, code and name values.

Have another class something WorkTypes.class and have an instance of a ArrayList of Work objects.

public class WorkTypes  {

    //worktypes here should match json...character to character!
    private ArrayList<Work> worktypes;

    public ArrayList<Work> getEntities() {
        return worktypes;
    }

    public void setEntities(ArrayList<Work> worktypes) {
        this.worktypes = worktypes;
    }
}

Then for GSON, do something like:

    WorkTypes objs = null;
    objs = gson.fromJson(jsonDataString, WorkTypes.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM