I have to sort a JSON array which contains JSON objects. The structure of the JSON is like the following:
[
{
"id": 582,
"isTransaction": false,
"toDate": "2015-08-26 16:12:00.0",
"fromDate": "2015-08-24 15:11:00.0",
"status": "REQUEST_ACCEPTED_BY_BOTH_SIDES"
},
{
"id": 602,
"isTransaction": false,
"toDate": "2015-08-21 21:52:00.0",
"fromDate": "2015-08-20 23:53:00.0",
"status": "REQUEST_ACCEPTED_BY_BOTH_SIDES"
}, {
"carFk": 1,
"endMileage": 1455,
"ownerName": "Celien",
"model": "335i",
"status": "DRIVER_SET_ODOMETER",
"ownerId": 1,
"toDate": "2015-08-26 16:12:00.0",
"startMileage": 455,
"id": 421,
"fromDate": "2015-08-24 15:11:00.0",
"isTransaction": true,
"brand": "Bmw",
"driverName": "Damien",
"exchange": false
}
]
What I have to do is to delete all The JSONObjects which have the same fromDate field. If two object have this same field, then I would like to keep the one which contains the field isTransaction:true
Here is the algorithm I wrote:
private void addOnlyRequestOrTransactionIntoListView(JSONArray array) {
JSONArray copyArray = array;
JSONArray resultArray = new JSONArray();
int i = 0, j = 0;
try {
while(i < array.length()){
JSONObject temp = array.getJSONObject(i);
j = 0;
while(j < copyArray.length()){
if(temp.getString("fromDate").equals(array.getJSONObject(j).getString("fromDate")))
if(temp.getBoolean("isTransaction"))
resultArray.put(temp);
j++;
}
i++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
But it does not what I want to performed. Can you give me some clues? Thanks!