android - Compare two JSONObject values and return the smaller between them -
for given json:
{ "month": { "days": { "day1": { "start": 13914323164815, "duration": 15 }, "day2": { "start": 13914123164815, "duration": 56 } } } }
i want values duration (in case 15 , 56), compare them , return smaller value. first got month object , tried next:
jsonobject days = mymonthobject.getjsonobject("days"); jsonarray daysarray = days.names(); (short r = 0; r<daysarray.length();r++){ string dayobject = daysarray.getstring(r); jsonobject alldaysobject = days.getjsonobject(dayobject); string duration = alldaysobject.getstring("duration"); log.w(tag, "the duration is: " + duration); }
on first iteration got message: duration is: 15, , on second iteration: duration is: 56.
now how preserve first found value duration , on next iteration of daysarray loop (the value 56) compare both , return , smaller?
any appreciated! :)
firstly, outisde loop, create int variable called max.
int max = 0;
essentially, you're getting value of each duration string (as in example):
string duration = alldaysobject.getstring("duration");
now, can int value of by:
int intduration = integer.parseint(duration);
next, compare intduration max:
if (intduration > max) max = intduration;
then can let iterate through entire array. @ end of loop, max hold highest duration.
Comments
Post a Comment