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

发布于 2024-12-08 07:27:44 字数 898 浏览 0 评论 0原文

我正在尝试解析来自 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 对象数组的名称。返回的每个对象都是一个“post”。但是,如果 JSON 对象数组没有名称,我该如何使用 GSON 解析它。我想将这些“帖子”放入 Post 类型的 ArrayList 中。

I am trying to parse data from a MongoDB cloud server. My JSON data returned from the server is as follows:

[
{
    "_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"
}

]

The problem I am having is the data structure returned does not include a name of the array of JSON objects. Each object returned is a "post". But how do I parse it using GSON if the there is no name for the array of JSON objects. I would like to put these "posts" into an ArrayList of type Post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晰传感 2024-12-15 07:27:44

您正在寻找的代码片段:

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

Piece of code you are looking for:

String jsonResponse = "bla bla bla";
Type listType = new TypeToken<List<Post>>(){}.getType();
List<Post> posts = (List<Post>) gson.fromJson(jsonResponse, listType);
居里长安 2024-12-15 07:27:44

使用 JSONArray 的构造函数来解析字符串:

//optionally use the com.google.gson.Gson package
Gson gson = new Gson();
ArrayList<Post> yourList = new ArrayList<Post>();
String jsonString = "your string";
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++){
  Post post = new Post();

  //manually parse for all 5 fields, here's an example for message
  post.setMessage(jsonArray.get(i).getString("message")); 

  //OR using gson...something like this should work
  //post = gson.fromJson(jsonArray.get(i),Post.class);

  yourList.Add(post);
 }

考虑到只有 5 个字段,使用 Gson 可能会比您需要的开销更多。

Use the contructor for JSONArray to parse the string:

//optionally use the com.google.gson.Gson package
Gson gson = new Gson();
ArrayList<Post> yourList = new ArrayList<Post>();
String jsonString = "your string";
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++){
  Post post = new Post();

  //manually parse for all 5 fields, here's an example for message
  post.setMessage(jsonArray.get(i).getString("message")); 

  //OR using gson...something like this should work
  //post = gson.fromJson(jsonArray.get(i),Post.class);

  yourList.Add(post);
 }

Considering there are only 5 fields, using Gson might be more overhead than you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文