For example, I get a chain of json like the following:
{
"error": false,
"entry_log": [651],
{
"id": 2073,
"hit": 2,
"amount": 3,
"pieces": 2,
"user_email": "xxxxx",
"log_time": "2015-05-07 13:05:21"
},
{
"id": 2069,
"hit": 1,
"amount": 3,
"pieces": 4,
"user_email": "xxxxx",
"log_time": "2015-05-07 12:05:25"
},
{
"id": 2065,
"hit": 0,
"amount": 0,
"pieces": 0,
"user_email": "xxxxx",
"log_time": "2015-05-06 09:05:38"
},
.
. (repeated sequence with the same or different log_time)
.
}
The above sequence is repeatedly updated by regular time interval, say 1 hour for one new json. What I am going to do is the sort out the sequences what are within the same day and sum up the hit and pieces from the entry_log and finally put them into an String arrays like becoming:
array1[0] = "2015-05-07" array1[1] = "2015-05-06" //log_time
array2[0] = "3" array2[1] = "0" //hit
array3[0] = "6" array3[1] = "0" // pieces
....and so on
Hence, I have the following code to sort them out:
JSONObject reader = new JSONObject(json); // this is the json of the above sequence
JSONArray entry_log = reader.getJSONArray("entry_log");
JSONObject inside_entry_log;
String[] date = new String[entry_log.length()];
String[] hit = new String[entry_log.length()];// I ignore this at this moment
String[] pieces = new String[entry_log.length()];
// I am quite unfamiliar with the array object, probably i should use arraylist here
int array_row = 0;
for (int i = 0; i < entry_log.length(); i++) {
inside_entry_log = entry_log.getJSONObject(i);
hit = inside_entry_log.getString("hit");
String new_pieces = inside_entry_log.getString("pieces");
String new_date = inside_entry_log.getString("log_time");
String[] Split2 = new_date.split("\\s+");
if (i == 0) {
date[0] = Split2[0];
pieces[0] = new_pieces;
}
if (i != 0) {
if (date[array_row].matches(Split2[0])) {
pieces[array_row] =
Float.toString(Float.parseFloat(pieces[array_row]) + Float.parseFloat(new_pieces));
} else {
array_row += 1;
date[array_row] = Split2[0];
pieces[array_row] = new_pieces;
}
}
}
But once the sequences become more and more, this slows down the running. So is it the only way to sort them or is there any smart way to fasten the sorting speed? Thank you so much for answering my question!!