0
{
  "transaction": {
    "id": 1,
    "empid": "12345",
    "details1": {
      "name": "xyz",
      "age": "30",
      "sex": "M",
      "Address": {
        "Office": "office",
        "Home": "Home"
      }
    },
    "abcDetails": "asdf",
    "mobile": 123455
  },
  "details2": {
    "id": 2,
    "empid": "64848",
    "details": {
      "name": "eryje",
      "age": 3027,
      "sex": "M",
      "Address": {
        "Office": "office",
        "Home": "Home"
      }
    },
    "abcDetails": "fhkdl",
    "mobile": 389928
  }
}

I am getting the data in above format. Here I did split and Iterating the data using loop. First time am getting below formatted data. So in this I want get name and age value and details1.Address.Office value also(keys are not static).

 "details1": {
        "name": "xyz",
        "age": "30",
        "sex": "M",
        "Address": {
            "Office": "office",
            "Home": "Home"
        }
    }
Jason Braucht
  • 2,358
  • 19
  • 31

1 Answers1

1

Try using JSONObject keys() to get the key and then iterate each key to get to the dynamic value.

// searchResult refers to the current element in the array "search_result"
JSONObject questionMark = searchResult.getJSONObject("question_mark");
Iterator keys = questionMark.keys();

while(keys.hasNext()) {
    // loop to get the dynamic key
    String currentDynamicKey = (String)keys.next();

    // get the value of the dynamic key
    JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);

    // do something here with the value...
}

Reference : How to parse a dynamic JSON key in a Nested JSON result?

Community
  • 1
  • 1
Narayanan S
  • 113
  • 1
  • 13