Java中的JSON数组只会导致最新的数组

时间:2018-07-20 23:52:57

标签: java json spring-boot

先生,早上好

我有这样的代码

00 00 00 26

}

我只是从我的代码中获得了sysout

00 00

在我的邮递员中,它只是返回最新的数组json

List<UserDeviceEntity> ude = userDeviceService.getAllUserByCompAndRole(ue.getCompanyId(), ue.getUserRoleId());
    System.out.println("roleid: "+ue.getUserRoleId());

    for(int i = 0; i < ude.size();i++){
        System.out.println("userid : "+ude.get(i).getUserId()); 


        List<UserDeviceEntity> uds = userDeviceService.getBikeId(ue.getUserId());
        System.out.println("compare userid: "+ue.getUserId());
        List<TBikeEntity> bikeServices = tbikeService.getAllLocation(ude.get(i).getUserId());
        System.out.println("size :"+bikeServices.size());

        if (ue.getUserId() == ude.get(i).getUserId()){
            List<TBikeEntity> bike = tbikeService.getAllLocation(ude.get(i).getBikeId());
            System.out.println("sizess bike 1 : "+bike.size());
            System.out.println("bikeidssssss : "+ude.get(i).getBikeId());   
        for (int j = 0; j < bike.size(); j++) {
            System.out.println("sizess : "+bike.size());
            obj1.put("bikeID",bike.get(j).getBikeId());
            obj1.put("imei",bike.get(j).getBikeImei());
            obj1.put("gpsTime",bike.get(j).getGpsLastUpdate());
            obj1.put("lo",bike.get(j).getBikeLongitude());
            obj1.put("la",bike.get(j).getBikeLatitude());
            obj1.put("gps_status",bike.get(j).getBikeGpsStatus());
            list.add(obj1);
        }
        obj.put("success", true);
        obj.put("data", list);

    }   

    }
    return obj;
}

为什么我的json返回仅显示BikeID:190,它似乎只是json中最新的数组显示,为什么我的第一个BikeID:182无法在我的第一个json结果中显示?预先感谢

1 个答案:

答案 0 :(得分:0)

在循环中,每次迭代都修改相同的obj1。 当您的list引用该对象时,您只会覆盖该对象,而不会添加新的对象。 尝试在第二个for循环中声明obj1。这样,每次迭代都会创建一个新对象。

List<UserDeviceEntity> ude = userDeviceService.getAllUserByCompAndRole(ue.getCompanyId(), ue.getUserRoleId());
    System.out.println("roleid: "+ue.getUserRoleId());

    for(int i = 0; i < ude.size();i++){
        System.out.println("userid : "+ude.get(i).getUserId()); 


        List<UserDeviceEntity> uds = userDeviceService.getBikeId(ue.getUserId());
        System.out.println("compare userid: "+ue.getUserId());
        List<TBikeEntity> bikeServices = tbikeService.getAllLocation(ude.get(i).getUserId());
        System.out.println("size :"+bikeServices.size());

        if (ue.getUserId() == ude.get(i).getUserId()){
            List<TBikeEntity> bike = tbikeService.getAllLocation(ude.get(i).getBikeId());
            System.out.println("sizess bike 1 : "+bike.size());
            System.out.println("bikeidssssss : "+ude.get(i).getBikeId());   
        for (int j = 0; j < bike.size(); j++) {
            System.out.println("sizess : "+bike.size());
           YourObjectTypeHere obj1 = new YourObjectTypeHere(); obj1.put("bikeID",bike.get(j).getBikeId());
            obj1.put("imei",bike.get(j).getBikeImei());
            obj1.put("gpsTime",bike.get(j).getGpsLastUpdate());
            obj1.put("lo",bike.get(j).getBikeLongitude());
            obj1.put("la",bike.get(j).getBikeLatitude());
            obj1.put("gps_status",bike.get(j).getBikeGpsStatus());
            list.add(obj1);
        }
        obj.put("success", true);
        obj.put("data", list);

    }   

    }
    return obj;
}