【问题标题】:JSON only display last element [closed]JSON仅显示最后一个元素[关闭]
【发布时间】:2021-02-08 04:43:22
【问题描述】:

我有一个项目是从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"
    }
  ]      
}

【问题讨论】:

  • 您发布的 JSON 是无效的。它包含无效的键。

标签: java arrays json


【解决方案1】:

你有一个错误的 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"
    }
  ]      
}

【讨论】:

    【解决方案2】:

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

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

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

    我还想在此处添加您正在使用的 json,如果它是 map,它将始终覆盖相同键的最后条目中的值 Map 不包含重复的键:值对

    【讨论】:

      猜你喜欢
      • 2014-11-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      相关资源
      最近更新 更多