Java – get value from JSONObject without mention its name

javajsonorg.json

My JSONObject:

{MyJSON:[{"id":"1","name":"panji","price":"100"}]}

I do this to get value of id:

menuitemArray.getJSONObject(i).getString("id");

Can I get the value of id, name or price, without mentioning its name, may be like index or another method?

Best Answer

You can call names() on a JSONObject to get a JSONArray of the names of its elements - this would let you do logic on them or retrieve their corresponding values dynamically.