GSON错误预期字符串但是找到了对象

时间:2014-04-01 09:43:18

标签: json gson

我使用gson反序列化这个对象:

[

        {
            "_id": "53357a63138c2564feaf187f",
            "worktypes": [
                {
                    "name": "Tipo uno"
                },
                {
                    "name": "Tipo dos"
                },
                {
                    "name": "dasdasd"
                }
            ],
            "company": "aa"
        },
        {
            "_id": "5335d03deffb9c0000b44304",
            "company": "asasdasd"
        },
        {
            "_id": "5339ba03ef04d1c5882f7aa2",
            "company": "Nueva"
        },
        {
            "_id": "5339c150ef04d1c5882f7aa3",
            "company": "Nueva"
        },
        {
            "_id": "5339c4f1ef04d1c5882f7aa4",
            "company": "Otra mas"
        },
        {
            "_id": "53340d07d6429d27e1284c77",
            "worktypes": [
                {
                    "name": "Pompas"
                },
                {
                    "name": "name 4"
                },
                {
                    "name": "Bombillos"
                },
                {
                    "name": "Pompas"
                },
                {
                    "name": "Bombillos 2"
                },
                {
                    "name": "uevvaa"
                },
                {
                    "name": "uevvaa"
                },
                {
                    "name": "aa"
                },
                {
                    "name": "ee"
                },
                {
                    "name": "ee"
                },
                {
                    "name": "ee"
                },
                {
                    "name": "ee"
                },
                {
                    "name": "ee"
                },
                {
                    "name": "ee"
                },
                {
                    "name": "asdasd"
                }
            ],
            "company": "Company2"
        }
    ]

使用此代码:

Gson gson = new Gson();
Type collectionType = new TypeToken<List<Company>>() {}.getType();
List<Company> lcs = (List<Company>) new Gson().fromJson(String.valueOf(value),collectionType);

公司包含companyString,worktypes list with worktpyes strcuture ..

怎么了?

1 个答案:

答案 0 :(得分:1)

准备这门课程:

class Company {
    String _id;
    List<WorkType> worktypes = new ArrayList<WorkType>();
    String company;

    class WorkType {
        String name;
    }
}

并使用此:

List<Company> companies = gson.fromJson(myJsonString, new TypeToken<List<Company>>(){}.getType());