JSONArray仅返回数据库的最后一项

时间:2018-07-04 09:16:46

标签: android arrays

我该如何JSONArray确认数据没有重复?

JSONObject parasTransDispatch = new JSONObject();
        JSONArray jArrayTransDispatch = new JSONArray();

        final List<String> dispControlNoArray = new ArrayList<>();
        final List<String> dispTripNoArray = new ArrayList<>();

        while (!cursorDispatch.isAfterLast()) {

            trans_dispatch_returnID = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_ID_RETURN));
            trans_dispatch_eodID = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_EOD_ID));
            trans_dispatch_routeID = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_ROUTE_ID));
            trans_dispatch_controlNo = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_CONTROL_NO));
            trans_dispatch_busNo = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_BUS_NO));
            trans_dispatch_tripNo = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_TRIP_NO));
            trans_dispatch_bound_trip = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_BOUND));
            trans_dispatch_route = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_ROUTE));
            trans_dispatch_trip_type = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_TRIP_TYPE));
            trans_dispatch_passCount = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_PASSENGER_COUNT));
            trans_dispatch_dispId = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_DISPATCHER_ID));
            trans_dispatch_conId = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_CONDUCTOR_ID));
            trans_dispatch_driId = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_DRIVER_ID));
            trans_dispatch_dri2Id = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_DRIVER2_ID));
            trans_dispatch_datetimeStamp = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_DATETIMESTAMP));
            trans_dispatch_long = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_LONG));
            trans_dispatch__lat = cursorDispatch.getString(cursorDispatch.getColumnIndex(DBHelper.TRANS_DISPATCH_LAT));

            dispControlNoArray.add("'" + trans_dispatch_controlNo + "'");
            dispTripNoArray.add("'" + trans_dispatch_tripNo + "'");

            try {
                parasTransDispatch.put("id", trans_dispatch_returnID);
                parasTransDispatch.put("eod_id", trans_dispatch_eodID);
                parasTransDispatch.put("route_id", trans_dispatch_routeID);
                parasTransDispatch.put("company_id", company_id);
                parasTransDispatch.put("subcompany_id", subCompanyID);
                parasTransDispatch.put("route_id", trans_dispatch_routeID);
                parasTransDispatch.put("control_no", trans_dispatch_controlNo);
                parasTransDispatch.put("bus_no", trans_dispatch_busNo);
                parasTransDispatch.put("trip_no", trans_dispatch_tripNo);
                parasTransDispatch.put("bound", trans_dispatch_bound_trip);
                parasTransDispatch.put("route", trans_dispatch_route);
                parasTransDispatch.put("trip_type", trans_dispatch_trip_type);
                parasTransDispatch.put("passenger_count", trans_dispatch_passCount);
                parasTransDispatch.put("dispatcher_id", trans_dispatch_dispId);
                parasTransDispatch.put("conductor_id", trans_dispatch_conId);
                parasTransDispatch.put("driver_id", trans_dispatch_driId);
                parasTransDispatch.put("driver2_id", trans_dispatch_dri2Id);
                parasTransDispatch.put("datetimestamp", trans_dispatch_datetimeStamp);
                parasTransDispatch.put("long", trans_dispatch_long);
                parasTransDispatch.put("lat", trans_dispatch__lat);


                cursorDispatch.moveToNext();

                jArrayTransDispatch.put(parasTransDispatch);


            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.e("jArrayTransDispatch", String.valueOf(jArrayTransDispatch));

这是我正在使用的代码,它向我返回最后一个数据,但计数正确吗?

这是日志

E/jArrayTransDispatch: [{"company_id":"4ed8954c-703e-41b5-94ba-eeb29546e3e3","eod_id":"d4c8d468-63a0-4e3c-9a32-ff880a8b946c","control_no":"11207042018000001","dispatcher_id":"b4d69ae7-2c96-42c6-bf9b-d007331b09a7","datetimestamp":"07\/04\/2018 14:53:10","conductor_id":"00c1e69c-e884-47fe-9be2-a4467aa745a0","id":"b2509b9b-2018-4a2d-962d-d613c087200d","passenger_count":"10","bus_no":"112","trip_type":"REGULAR TRIP","route":"BINAN - BUENDIA","route_id":"1901d3b3-36dd-4f62-b614-adc06e4692b4","trip_no":"4","driver_id":"75459e1c-4352-4650-a7e2-15094c80b1c2","subcompany_id":"2d4d8dc5-e353-444f-ada6-e6d7464fd856","bound":"NORTH"},

{"company_id":"4ed8954c-703e-41b5-94ba-eeb29546e3e3","eod_id":"d4c8d468-63a0-4e3c-9a32-ff880a8b946c","control_no":"11207042018000001","dispatcher_id":"b4d69ae7-2c96-42c6-bf9b-d007331b09a7","datetimestamp":"07\/04\/2018 14:53:10","conductor_id":"00c1e69c-e884-47fe-9be2-a4467aa745a0","id":"b2509b9b-2018-4a2d-962d-d613c087200d","passenger_count":"10","bus_no":"112","trip_type":"REGULAR TRIP","route":"BINAN - BUENDIA","route_id":"1901d3b3-36dd-4f62-b614-adc06e4692b4","trip_no":"4","driver_id":"75459e1c-4352-4650-a7e2-15094c80b1c2","subcompany_id":"2d4d8dc5-e353-444f-ada6-e6d7464fd856","bound":"NORTH"},

{"company_id":"4ed8954c-703e-41b5-94ba-eeb29546e3e3","eod_id":"d4c8d468-63a0-4e3c-9a32-ff880a8b946c","control_no":"11207042018000001","dispatcher_id":"b4d69ae7-2c96-42c6-bf9b-d007331b09a7","datetimestamp":"07\/04\/2018 14:53:10","conductor_id":"00c1e69c-e884-47fe-9be2-a4467aa745a0","id":"b2509b9b-2018-4a2d-962d-d613c087200d","passenger_count":"10","bus_no":"112","trip_type":"REGULAR TRIP","route":"BINAN - BUENDIA","route_id":"1901d3b3-36dd-4f62-b614-adc06e4692b4","trip_no":"4","driver_id":"75459e1c-4352-4650-a7e2-15094c80b1c2","subcompany_id":"2d4d8dc5-e353-444f-ada6-e6d7464fd856","bound":"NORTH"},

{"company_id":"4ed8954c-703e-41b5-94ba-eeb29546e3e3","eod_id":"d4c8d468-63a0-4e3c-9a32-ff880a8b946c","control_no":"11207042018000001","dispatcher_id":"b4d69ae7-2c96-42c6-bf9b-d007331b09a7","datetimestamp":"07\/04\/2018 14:53:10","conductor_id":"00c1e69c-e884-47fe-9be2-a4467aa745a0","id":"b2509b9b-2018-4a2d-962d-d613c087200d","passenger_count":"10","bus_no":"112","trip_type":"REGULAR TRIP","route":"BINAN - BUENDIA","route_id":"1901d3b3-36dd-4f62-b614-adc06e4692b4","trip_no":"4","driver_id":"75459e1c-4352-4650-a7e2-15094c80b1c2","subcompany_id":"2d4d8dc5-e353-444f-ada6-e6d7464fd856","bound":"NORTH"},

{"company_id":"4ed8954c-703e-41b5-94ba-eeb29546e3e3","eod_id":"d4c8d468-63a0-4e3c-9a32-ff880a8b946c","control_no":"11207042018000001","dispatcher_id":"b4d69ae7-2c96-42c6-bf9b-d007331b09a7","datetimestamp":"07\/04\/2018 14:53:10","conductor_id":"00c1e69c-e884-47fe-9be2-a4467aa745a0","id":"b2509b9b-2018-4a2d-962d-d613c087200d","passenger_count":"10","bus_no":"112","trip_type":"REGULAR TRIP","route":"BINAN - BUENDIA","route_id":"1901d3b3-36dd-4f62-b614-adc06e4692b4","trip_no":"4","driver_id":"75459e1c-4352-4650-a7e2-15094c80b1c2","subcompany_id":"2d4d8dc5-e353-444f-ada6-e6d7464fd856","bound":"NORTH"}]

2 个答案:

答案 0 :(得分:1)

JSONObject parasTransDispatch = new JSONObject();

必须在循环的每次迭代中实例化。

答案 1 :(得分:1)

您应该像@Blackbelt所说的那样在迭代循环中实例化新对象,然后再编写此行

cursorDispatch.moveToNext();

那么您的输出将是1,2,3,4