JSON 只显示最后一个元素

时间:2021-02-08 04:43:22

标签: java arrays json

我有一个项目要从json中检索数据,但是出来的数据只是最后一部分。几个论坛解释了覆盖值。但我还是不明白怎么解决,或者我在数组捕获上错了

JSONArray arrayMaps = jsonObject.getJSONArray("Maps");
                for (int i = 0; i < arrayMaps.length(); i++){

                    Model us = new Model();
                    JSONObject c = arrayMaps.getJSONObject(i);
                    us.setTitle(c.getString("judul_maps"));
                    us.setUrl(c.getString("img_maps"));
                    us.setContent(c.getString("desk_maps"));
                    us.setDownloads(c.getString("donlot_maps"));

                    listMap.add(us);
                }

listMap 是一个数组列表

这是我的 json

{ 
  "Maps": [
    {
      
      "judul_maps": "Mario Bross Maps 1x",
      "img_maps": "https://i.ytimg.com/vi/e8N7oPv_MCs/maxresdefault.jpg",
      "desk_maps": "x1 Super Mario Bros MAPS-1 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://archive.org/download/mariobross_maps/mariobross_maps.zip",
      
      
      "judul_maps": "Mario Bross Maps 2x",
      "img_maps": "https://i.ytimg.com/vi/iZ5dbVvprkQ/maxresdefault.jpg",
      "desk_maps": "x2 Super Mario Bros MAPS-2 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://matix.li/951a59821a86",
      
      
      "judul_maps": "Mario Bross Maps 3",
      "img_maps": "https://i.ytimg.com/vi/lbPvLzB8ifc/maxresdefault.jpg",
      "desk_maps": "Super Mario Bros MAPS-3 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://matix.li/bf6ce47f3b2e",
      
      
      "judul_maps": "Mario Bross Maps 4",
      "img_maps": "https://i.ytimg.com/vi/iKyfNrksGeA/maxresdefault.jpg",
      "desk_maps": "Super Mario Bros MAPS-4 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://matix.li/cf70364bac8a",
      
      
      "judul_maps": "Mario Bross Maps 5",
      "img_maps": "https://i.ytimg.com/vi/nMIW-BoKdU4/maxresdefault.jpg",
      "desk_maps": "Super Mario Bros MAPS-5 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://matix.li/5cca3c7f677e"
    }
  ]      
}

2 个答案:

答案 0 :(得分:0)

您输入了错误的 Json。 JsonArray 中只有一个 Json。该 Json 具有相同的密钥,它将覆盖顶部消息。 你应该像这样写你的 Json。

{ 
  "Maps": [
    {
      
      "judul_maps": "Mario Bross Maps 1x",
      "img_maps": "https://i.ytimg.com/vi/e8N7oPv_MCs/maxresdefault.jpg",
      "desk_maps": "x1 Super Mario Bros MAPS-1 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://archive.org/download/mariobross_maps/mariobross_maps.zip"
    },
      
    { 
      "judul_maps": "Mario Bross Maps 2x",
      "img_maps": "https://i.ytimg.com/vi/iZ5dbVvprkQ/maxresdefault.jpg",
      "desk_maps": "x2 Super Mario Bros MAPS-2 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://matix.li/951a59821a86"
    },
      
    {  
      "judul_maps": "Mario Bross Maps 3",
      "img_maps": "https://i.ytimg.com/vi/lbPvLzB8ifc/maxresdefault.jpg",
      "desk_maps": "Super Mario Bros MAPS-3 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://matix.li/bf6ce47f3b2e"
    },
      
    {  
      "judul_maps": "Mario Bross Maps 4",
      "img_maps": "https://i.ytimg.com/vi/iKyfNrksGeA/maxresdefault.jpg",
      "desk_maps": "Super Mario Bros MAPS-4 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://matix.li/cf70364bac8a"
    },
      
    {  
      "judul_maps": "Mario Bross Maps 5",
      "img_maps": "https://i.ytimg.com/vi/nMIW-BoKdU4/maxresdefault.jpg",
      "desk_maps": "Super Mario Bros MAPS-5 for Minecraft is a parkour map created by jaxot. About the story, Mario and the Princess are walking peacefully in the park.",
      "donlot_maps": "https://matix.li/5cca3c7f677e"
    }
  ]      
}

答案 1 :(得分:0)

您传递的 Json 对象本身覆盖了值,这是对象数组,因此您应该包含对象而不是具有不同对象的同名字段 要创建有效的 Json 对象,您可以尝试 https://jsonformatter.org/ 以便您了解您正在使用有效的对象或不..

你的 Json 对象必须是这样的:

{
  "myObj" : [
      {
        "id": 1,
        "name":"firstEle"
      },
     {
       "id" :2,
       "name":"secondEle"
     }
   ]
}

有一件事我还想在这里添加你正在使用的 json,如果它是映射,它总是会覆盖相同键的最后一个条目的值映射不包含重复的键:值对