2

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!!

2
  • Please edit your question to include the tag for the relevant programming language. Commented May 7, 2015 at 8:00
  • Dexter, why have you removed all these commas? Now this is not a valid JSON anymore. Commented May 7, 2015 at 10:25

2 Answers 2

3

@Dexter .. first i found your json bit typical..as you put

 "entry_log": [
    651
]

whereas entry_log just contain single int..so why jsonarray

you can first format your json like this

{
"error": false,
"entry_log": 651,
"log": [
    {
        "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"
    }
  ]
}

then you can kept the whole jsonarray of "log" to jsonArray object or serialize to the java object

create equivalent object for this json eg-

public class LogModal {
/**
 * entry_log : 651
 * log : [{"pieces":2,"hit":2,"amount":3,"user_email":"xxxxx","id":2073,"log_time":"2015-05-07 13:05:21"},{"pieces":4,"hit":1,"amount":3,"user_email":"xxxxx","id":2069,"log_time":"2015-05-07 12:05:25"},{"pieces":0,"hit":0,"amount":0,"user_email":"xxxxx","id":2065,"log_time":"2015-05-06 09:05:38"}]
 * error : false
 */
private int entry_log;
private List<LogEntity> log;
private boolean error;

public void setEntry_log(int entry_log) {
    this.entry_log = entry_log;
}

public void setLog(List<LogEntity> log) {
    this.log = log;
}

public void setError(boolean error) {
    this.error = error;
}

public int getEntry_log() {
    return entry_log;
}

public List<LogEntity> getLog() {
    return log;
}

public boolean isError() {
    return error;
}

public class LogEntity {
    /**
     * pieces : 2
     * hit : 2
     * amount : 3
     * user_email : xxxxx
     * id : 2073
     * log_time : 2015-05-07 13:05:21
     */
    private int pieces;
    private int hit;
    private int amount;
    private String user_email;
    private int id;
    private String log_time;

    public void setPieces(int pieces) {
        this.pieces = pieces;
    }

    public void setHit(int hit) {
        this.hit = hit;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public void setUser_email(String user_email) {
        this.user_email = user_email;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setLog_time(String log_time) {
        this.log_time = log_time;
    }

    public int getPieces() {
        return pieces;
    }

    public int getHit() {
        return hit;
    }

    public int getAmount() {
        return amount;
    }

    public String getUser_email() {
        return user_email;
    }

    public int getId() {
        return id;
    }

    public String getLog_time() {
        return log_time;
    }
}
}

then use library like jackson/gson to serialize to the java object which helps to fasten your app

ex-

LogModal logmodal=gson.fromJson(jsonString, LogModal);
Sign up to request clarification or add additional context in comments.

Comments

1

Note : First fix your json response issue. Refer @Angad Tiwari's answer and for below code json data is took from his answer.

Now to find out counts of each day follow this: Get all unique date

public class LogModal {

    private int entry_log;
    private List<LogEntity> log;
    private boolean error;

    //getter and setter goes here

    List<String> dateList = new ArrayList<String>();
    //get list of unique date from your response
    public List<String> getUniqueDateList() {
        for (LogEntity logEntity : getLog()) {

            String arrayDate = MainActivity.getFormattedDate(logEntity.getLog_time());

            if (!dateList.contains(arrayDate)) {
                dateList.add(arrayDate);
            }
        }
        return dateList;
    }

}

Counting according to date MainActivity.java

    Gson gson = new Gson();
    LogModal logModal = gson.fromJson(response, LogModal.class);

    List<LogEntity> logEntries = logModal.getLog();

    List<DayEntry> dayEntries = new ArrayList<DayEntry>();

    for (String uDate : logModal.getUniqueDateList()) {
        DayEntry entry = new DayEntry();
        long hit = 0, amount = 0, pieces = 0;

        for (LogEntity e : logEntries) {
            String logDate = getFormattedDate(e.getLog_time());
            //counting each day records
            if (compareDate(uDate, logDate)) {
                hit += e.getHit();
                pieces += e.getPieces();
                amount += e.getAmount();
            }
        }
        entry.date = uDate;
        entry.amount = amount;
        entry.pieces = pieces;
        entry.hit = hit;
        dayEntries.add(entry);
    }



    for (DayEntry dayEntity : dayEntries) {
        Log.i("TAG"," Date : "+dayEntity.getDate()+" Hits : "+dayEntity.getHit()+" Amount : "+dayEntity.getAmount());
    }

Date related functions MainActivity.java

//remove hour, minute, second from date 
public static String getFormattedDate(String date) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");

    SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd");
    Date d1;
    try {
        d1 = formatter.parse(date);
        String wDate = date1.format(d1);
        return wDate;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;

}

//compare date
public boolean compareDate(String date1, String date2) {
    return date1.equals(date2);
}

Output :
 Date : 2015-05-07 Hits : 3 Amount : 6
 Date : 2015-05-06 Hits : 0 Amount : 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.