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")?
-
4in this particular case you do not get a json object, but a stringBlackbelt– Blackbelt2013年07月24日 13:10:30 +00:00Commented Jul 24, 2013 at 13:10
-
3@blackbelt is right. Maybe the service should be returning {"Preferences":{"Pref1" : "Apple", "Pref2":"Pear"}}.Eric Levine– Eric Levine2013年07月24日 13:15:01 +00:00Commented Jul 24, 2013 at 13:15
4 Answers 4
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);
}
Comments
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"));
}
4 Comments
JSONException at json.getString("Pref1") and json.getString("Pref2")since Pref1 and Pref2 are not mapped.JSONException because you used =instead of : ;)["Preferences":{"Pref1":"Juan", "Pref2":"JuanK"}]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)
Comments
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());
}