使用GSON解析JSON数组

时间:2021-02-12 13:42:46

I have a JSON file like this:

我有一个这样的JSON文件:

[
    {
        "number": "3",
        "title": "hello_world",
    }, {
        "number": "2",
        "title": "hello_world",
    }
]

Before when files had a root element I would use:

在文件有根元素之前,我会使用:

Wrapper w = gson.fromJson(JSONSTRING, Wrapper.class);

code but I can't think how to code the Wrapper class as the root element is an array.

代码,但我不知道如何将包装器类编码为根元素是一个数组。

I have tried using:

我有试着用:

Wrapper[] wrapper = gson.fromJson(jsonLine, Wrapper[].class);

with:

:

public class Wrapper{

    String number;
    String title;

}

But haven't had any luck. How else can I read this using this method?

但是没有任何运气。用这种方法我还能怎么读?

P.S I have got this to work using:

P。我用这个来工作:

JsonArray entries = (JsonArray) new JsonParser().parse(jsonLine);
String title = ((JsonObject)entries.get(0)).get("title");

But I would prefer to know how to do it (if possible) with both methods.

但是我更希望知道如何(如果可能的话)用这两种方法来做。

3 个解决方案

#1


90  

Problem is caused by comma after last element of your array (after each title). If you remove it and change your data to

问题是在数组的最后一个元素(每个标题之后)后面加上逗号。如果您删除它并将数据更改为

[
    {
        "number": "3",
        "title": "hello_world"
    }, {
        "number": "2",
        "title": "hello_world"
    }
]

Wrapper[] data = gson.fromJson(jElement, Wrapper[].class); will work fine.

数据= gson.fromJson(jElement, Wrapper[].class);将正常工作。

#2


35  

Gson gson = new Gson();
Wrapper[] arr = gson.fromJson(str, Wrapper[].class);

class Wrapper{
    int number;
    String title;       
}

Seems to work fine. But there is an extra , Comma in your string.

似乎工作好。但是在你的字符串中有一个额外的逗号。

[
    { 
        "number" : "3",
        "title" : "hello_world"
    },
    { 
        "number" : "2",
        "title" : "hello_world"
    }
]

#3


9  

public static <T> List<T> toList(String json, Class<T> clazz) {
    if (null == json) {
        return null;
    }
    Gson gson = new Gson();
    return gson.fromJson(json, new TypeToken<T>(){}.getType());
}

sample call:

示例调用:

List<Specifications> objects = GsonUtils.toList(products, Specifications.class);

#1


90  

Problem is caused by comma after last element of your array (after each title). If you remove it and change your data to

问题是在数组的最后一个元素(每个标题之后)后面加上逗号。如果您删除它并将数据更改为

[
    {
        "number": "3",
        "title": "hello_world"
    }, {
        "number": "2",
        "title": "hello_world"
    }
]

Wrapper[] data = gson.fromJson(jElement, Wrapper[].class); will work fine.

数据= gson.fromJson(jElement, Wrapper[].class);将正常工作。

#2


35  

Gson gson = new Gson();
Wrapper[] arr = gson.fromJson(str, Wrapper[].class);

class Wrapper{
    int number;
    String title;       
}

Seems to work fine. But there is an extra , Comma in your string.

似乎工作好。但是在你的字符串中有一个额外的逗号。

[
    { 
        "number" : "3",
        "title" : "hello_world"
    },
    { 
        "number" : "2",
        "title" : "hello_world"
    }
]

#3


9  

public static <T> List<T> toList(String json, Class<T> clazz) {
    if (null == json) {
        return null;
    }
    Gson gson = new Gson();
    return gson.fromJson(json, new TypeToken<T>(){}.getType());
}

sample call:

示例调用:

List<Specifications> objects = GsonUtils.toList(products, Specifications.class);