Iterate over JSONObject

I am analyzing a response from a web service to a JSONObject , which upon registration looks like this:

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

I understand how to request a Preferences tag, for example. jsonObject.get("Preferences") . However, I do not understand which object I am returning and how to iterate over it. How can I jsonObject.get("Preferences") over the object returned by jsonObject.get("Preferences") ?

+4
source share
5 answers

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); } 
+10
source

You cannot go through the above JsonObject, but if it was like

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

you could do 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")); } 
+2
source

You cannot JSONObject over JSONObject because it is not an array. You should get a JSONArray object, but you cannot, because what you are showing is not a valid JSONArray .

if you want it to be an array, you need to get something like this:

 { "Preferences": [ "Juan" , "Juank" ]} 
0
source

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

0
source

try it

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

If it is an array, then

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

Source: https://habr.com/ru/post/1493196/


All Articles