Home » Java » Java Extract data from JSON Array

Java Extract data from JSON Array

Posted by: admin July 12, 2018 Leave a comment


Below is the json format, I have to extract the data of dynamic key-value pair.

{"data": [
         {"1000":"2018-03-10 00:00:00.0"},
         {"100031": "2018-03-15"}

This is something I am trying but its not working and showing errors:

In input, I am getting the JSON object.

JSONArray lastdtArr =   json.getJSONArray("data");
int len = lastdtArr.length();
for(int i=0;i<len;i++) {
    JSONObject lastdtid = lastdtArr.getJSONObject(i);
    int id = lastdtid.getKey();
    String date = lastdtid.getValue();
    //Operations to perform

PS: I am completely new to Java. The solutions I find over internet are for the json format where key is fixed in key-value pair. But in this case, key is always dynamic. Please let me know what all I have to write inside the loop for getting the values in id and date?