Vishal Anand
Vishal Anand

Reputation: 23

How can I get last element of JSON Object without specifying key value?

Here is the data of covid 19 coming from that API: https://data.covid19india.org/v4/min/timeseries.min.json I want to reach the last element / or "2020-03-06" of "dates" JSON Object in JAVA / Android Studio.

If anyone can suggest to me that how can I reach the last element of this JSON Object without converting or changing it into JSON Array.

"TT": {
"dates": {
  "2020-01-30": {
    "delta": {
      "confirmed": 1
    },
    "delta7": {
      "confirmed": 1
    },
    "total": {
      "confirmed": 1
    }
  },
  "2020-02-02": {
    "delta": {
      "confirmed": 1
    },
    "delta7": {
      "confirmed": 2
    },
    "total": {
      "confirmed": 2
    }
  },
  "2020-03-03": {
    "delta": {
      "confirmed": 1
    },
    "delta7": {
      "confirmed": 3
    },
    "total": {
      "confirmed": 6,
      "recovered": 3
    }
  },
  "2020-03-04": {
    "delta": {
      "confirmed": 22
    },
    "delta7": {
      "confirmed": 25
    },
    "total": {
      "confirmed": 28,
      "recovered": 3
    }
  },
  "2020-03-05": {
    "delta": {
      "confirmed": 2
    },
    "delta7": {
      "confirmed": 27
    },
    "total": {
      "confirmed": 30,
      "recovered": 3
    }
  },
  "2020-03-06": {
    "delta": {
      "confirmed": 1
    },
    "delta7": {
      "confirmed": 28
    },
    "total": {
      "confirmed": 31,
      "recovered": 3
    }
 }

Upvotes: 1

Views: 1739

Answers (1)

Omar Shawky
Omar Shawky

Reputation: 1268

get the JSONObject for dates first and then get the last item by getting a JSONArray on its names(keys), since you have an array of its keys now you can get the last item that exist at arr.length()-1.
like this:

JSONObject dates = jsonObject.getJSONObject("TT").getJSONObject("dates");
JSONArray arr  = dates.names();
String lastDate = arr2.getString(arr.length()-1);

from there you can do whatever since you have the needed key.

by the way your JSON is missing some brackets which bugged me for some time

{"TT": {
"dates": {
  "2020-01-30": {
    "delta": {
      "confirmed": 1
    },
    "delta7": {
      "confirmed": 1
    },
    "total": {
      "confirmed": 1
    }
  },
  "2020-02-02": {
    "delta": {
      "confirmed": 1
    },
    "delta7": {
      "confirmed": 2
    },
    "total": {
      "confirmed": 2
    }
  },
  "2020-03-03": {
    "delta": {
      "confirmed": 1
    },
    "delta7": {
      "confirmed": 3
    },
    "total": {
      "confirmed": 6,
      "recovered": 3
    }
  },
  "2020-03-04": {
    "delta": {
      "confirmed": 22
    },
    "delta7": {
      "confirmed": 25
    },
    "total": {
      "confirmed": 28,
      "recovered": 3
    }
  },
  "2020-03-05": {
    "delta": {
      "confirmed": 2
    },
    "delta7": {
      "confirmed": 27
    },
    "total": {
      "confirmed": 30,
      "recovered": 3
    }
  },
  "2020-03-06": {
    "delta": {
      "confirmed": 1
    },
    "delta7": {
      "confirmed": 28
    },
    "total": {
      "confirmed": 31,
      "recovered": 3
    }
 }
}
}
}

Upvotes: 0

Related Questions