如何使用GSON解析此JSON数据?并将其放入ArrayList中

IZI*_*IZI 4 parsing android json mongodb gson

我正在尝试解析MongoDB云服务器中的数据.我从服务器返回的JSON数据如下:

[
{
    "_id": {
        "$oid": "4e78eb48737d445c00c8826b"
    },
    "message": "cmon",
    "type": 1,
    "loc": {
        "longitude": -75.65530921666667,
        "latitude": 41.407904566666666
    },
    "title": "test"
},
{
    "_id": {
        "$oid": "4e7923cb737d445c00c88289"
    },
    "message": "yo",
    "type": 4,
    "loc": {
        "longitude": -75.65541383333333,
        "latitude": 41.407908883333334
    },
    "title": "wtf"
},
{
    "_id": {
        "$oid": "4e79474f737d445c00c882b2"
    },
    "message": "hxnxjx",
    "type": 4,
    "loc": {
        "longitude": -75.65555572509766,
        "latitude": 41.41263961791992
    },
    "title": "test cell"
}
Run Code Online (Sandbox Code Playgroud)

]

我遇到的问题是返回的数据结构不包含JSON对象数组的名称.返回的每个对象都是"帖子".但是如果没有JSON对象数组的名称,我如何使用GSON解析它.我想将这些"帖子"放入Post类型的ArrayList中.

yor*_*rkw 13

您正在寻找的一段代码:

String jsonResponse = "bla bla bla";
Type listType = new TypeToken<List<Post>>(){}.getType();
List<Post> posts = (List<Post>) gson.fromJson(jsonResponse, listType);
Run Code Online (Sandbox Code Playgroud)