the object returned by preferences is String . If you want to iterate over child elements of Preferences, you might want to change the structure, for example:
{"Preferences":{"Pref1":"Apple", "Pref2":"Pear"}}
and analyze it as follows:
JSONObject inputJSON = new JSONObject("{\"Preferences\":{\"Pref1\":\"Apple\", \"Pref2\":\"Pear\"}}"); JSONObject preferencesJSON = inputJSON.getJSONObject("Preferences"); Iterator<String> keysIterator = preferencesJSON.keys(); while (keysIterator.hasNext()) { String keyStr = (String)keysIterator.next(); String valueStr = preferencesJSON.getString(keyStr); }
alternatively, if you want to keep your structure, you can parse the returned string with the Preferences object as follows:
JSONObject inputJSON = new JSONObject("{\"Preferences\":\"{Pref1=Apple, Pref2=Pear}\"}"); String preferencesStr = inputJSON.getString("Preferences"); JSONObject preferencesJSON = new JSONObject(preferencesStr); Iterator<String> keysIterator = preferencesJSON.keys(); while (keysIterator.hasNext()) { String keyStr = (String)keysIterator.next(); String valueStr = preferencesJSON.getString(keyStr); }
source share