gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:07:26 27 4
gpt4 key购买 nike

我正在尝试解析来自 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"
}

]

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

最佳答案

您正在寻找的代码 fragment :

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

关于android - 如何使用 GSON 解析此 JSON 数据?并将其放入 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7653033/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com