2

I am trying to parse a JSON file in the following structure

{
"category": {
    "subcat_1": [
        {
            "item1": "xx"
        },
        {
            "item2": "xx"
        }
    ],
    "subcat_2": [
        {
            "item1": "xx"
        },
        {
            "item2": "xx"
        }
    ]
  }
}

It is working when am using getJSONArray("subcat_1") but, i want to get these string without the need to pre-save them in an array { subcat_1, subcat_2, ... } to maintain the dynamic feature?

0

1 Answer 1

2

You can get the keys for the object using keys(), which returns an Iterator:

Iterator it = json.keys();
while(it.hasNext()) {
  String key = (String)it.next();
  JSONObject current = json.getJSONObject(key);
  // do something with current
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.