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.9k6 gold badges52 silver badges77 bronze badges
asked Jul 24, 2013 at 13:08
2
  • 4
    in this particular case you do not get a json object, but a string Commented Jul 24, 2013 at 13:10
  • 3
    @blackbelt is right. Maybe the service should be returning {"Preferences":{"Pref1" : "Apple", "Pref2":"Pear"}}. Commented Jul 24, 2013 at 13:15

4 Answers 4

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);
}
answered Jul 24, 2013 at 13:13
Sign up to request clarification or add additional context in comments.

Comments

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"));
}
answered Jul 24, 2013 at 13:19

4 Comments

This would probably (not tested) throw a JSONException at json.getString("Pref1") and json.getString("Pref2")since Pref1 and Pref2 are not mapped.
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.
Now it will throw a JSONException because you used =instead of : ;)
You probably want:["Preferences":{"Pref1":"Juan", "Pref2":"JuanK"}]
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)

answered Jul 24, 2013 at 13:13

Comments

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());
 }
answered Jul 24, 2013 at 13:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.