Reputation: 81
I have the following JSON below I want to retrieve the value of the that last dateTime at the end 2011-05-01
{
"activities-log-steps":[
{"dateTime":"2011-04-27","value":5490},
{"dateTime":"2011-04-28","value":2344},
{"dateTime":"2011-04-29","value":2779},
{"dateTime":"2011-04-30","value":9196},
{"dateTime":"2011-05-01","value":15828},
{"dateTime":"2011-05-02","value":1945},
{"dateTime":"2011-05-03","value":366}
]
}
I have included the code for the current method I am using to try and retrieve that value.
protected void onPostExecute(JSONObject jsonObject) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = format1.format(cal.getTime());
if (jsonObject != null) {
try {
name = jsonObject.getString("dateTime" + ":" + "" + currentDate + "," + "value");
Toast.makeText(getActivity(), name, Toast.LENGTH_LONG).show();
Log.d("Token Access", name);
// _access.setText("Access Token:" + heartrate);
new refresh().execute();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(getActivity(), "Network Error", Toast.LENGTH_SHORT).show();
}
}
}
This is not returning a value
Upvotes: 0
Views: 2239
Reputation: 6527
JSONArray ar = jsonObject.getJSONArray("activities-log-steps");
JSONObject lastObj = ar.getJSONObject(ar.size()-1);
String dateTime = lastObj.getString("dateTime");
String value = lastObj.getString("value");
You need to locate the last Object from the array activities-log-steps
and then extract your JSON vars.
Upvotes: 1