4

I parse the response from a web service into a JSONObject, which when logged, looks as follows:

{"Preferences":"{Pref1=Apple, Pref2=Pear}"}

I understand how to ask for the Preferences tag e.g. jsonObject.get("Preferences"). However, I do not understand what object I am getting back nor how to iterate over it. How can I iterate over the object returned by jsonObject.get("Preferences")?

James Chevalier
  • 10,604
  • 5
  • 48
  • 74
RunLoop
  • 20,288
  • 21
  • 96
  • 151

4 Answers4

12

the object returned by preferences is a String. If you want to iterate through the childs of Preferences you may want to change the structure, to for example:

{"Preferences":{"Pref1":"Apple", "Pref2":"Pear"}}

and parse it like this:

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 by the Preferences object like this:

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);
}
Daniel
  • 20,420
  • 10
  • 92
  • 149
2

You can't iterate through above JsonObject, but if it were like this

["Preferences":{"Pref1":"Juan", "Pref2":"JuanK"}]

you could have done like

JSONArray array = new JSONArray(dataInStringForm);

for (int i=0;i< array.length(); i++)
{
   JSONObject json = array.getJsonObject(i);

   System.out.println(json.getString("Pref1"));
   System.out.println(json.getString("Pref2"));
}
Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • This would probably (not tested) throw a `JSONException` at `json.getString("Pref1")` and `json.getString("Pref2")`since Pref1 and Pref2 are not mapped. – Daniel Jul 24 '13 at 13:35
  • I know every string is enclosed in quotes "" when the json data is returned from internet, so this code won't give any exception as long as the JsonData is properly formated. – Umer Farooq Jul 24 '13 at 13:45
  • Now it will throw a `JSONException` because you used `=`instead of `:` ;) – Daniel Jul 24 '13 at 13:55
  • You probably want:`["Preferences":{"Pref1":"Juan", "Pref2":"JuanK"}]` – Daniel Jul 24 '13 at 14:02
0

You can parse values from JSONObject and can use getJSONObject(i) in a loop if it's a JSONArray. In your case you should make your preferences a JSONArray [{"Pref1":"Juan"},{"Pref2":"JuanK"}] so you can get it via getJSONArray() and then parse it better in a loop via getJSONObject(i)

Onur A.
  • 3,007
  • 3
  • 22
  • 37
0

Try this

Iterator<String> iter = json.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            try {
                Object value = json.get(key);
            } catch (JSONException e) {

            }
        }

If it is array then

JSONArray key= (JSONArray) jsonObject.get("yourKey");
        Iterator<String> iterator = key.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }