Reputation: 755
i read a json string into a JSONObject in java. The json-String varies:
This is one json:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
This is the same if i get it 5 Minutes later:
{"menu": {
"id": "file",
"popup": {
"menuitem": [
{"master": "New", "onclick": "CreateNewDoc()"},
]
}
}}
The json has not the same structure as the first one. I am not able to change the server side. This is given in the project.
So i like to create a list of all values stored in the json without knowing the keys:
Key, Value
menu-id, file
menu-value, File
menu-popup-menuitem-0-value, New
menu-popup-menuitem-0-onclick, CreateNewDoc
...
But if i use JSONObject i have to know the keys. I did not find any funciton to loop through alle elements?
Can somebody help?
This is my code:
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;)
sb.append((char)c);
String response = sb.toString();
JSONObject json_response = new JSONObject(response);
how to loop throug json_response to get each key/value and check if it has elements below????
Upvotes: 3
Views: 2949
Reputation: 457
Changing this:
//if current value is array, iterate it
JSONArray array = (JSONArray) value;
for (int i = 0; i < array.length(); i++) {
Object object = array.get(i);
JSONObject jsonObject = (JSONObject) object;
//assuming current array member is object, call recursively with next key + index and current member
recursiveTraverse(nextKey + "-" + i, jsonObject);
//you might need to handle special case of current member being array
}
to this:
//if current value is array, iterate it
JSONArray array = (JSONArray) value;
for (int i = 0; i < array.length(); i++) {
Object object = array.get(i);
if (object instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) object;
//assuming current array member is object, call recursively with next key + index and current member
recursiveTraverse(nextKey + "." + i, jsonObject);
} else {
//value is neither object, nor array, so we print and this ends the recursion
System.out.println(nextKey + ", " + object);
}
}
Resolved the issue when the current array member is an object.
Upvotes: 1
Reputation: 6985
JSONObject is like a java.util.Map, even if it does not implement the interface. You can take all keys and iterate through them even if you do not know them beforehand.
jsonObject.keySet()
This returns Set, consisting of the keys in the object. Now the recursive function:
private static void recursiveTraverse(String previousKey, JSONObject currentObject) {
//iterate each key
for (String currentKey : currentObject.keySet()) {
//build the next key
String nextKey = previousKey == null || previousKey.isEmpty() ? currentKey : previousKey + "-" + currentKey;
Object value = currentObject.get(currentKey);
if (value instanceof JSONObject) {
//if current value is object, call recursively with next key and value
recursiveTraverse(nextKey, (JSONObject) value);
} else if (value instanceof JSONArray) {
//if current value is array, iterate it
JSONArray array = (JSONArray) value;
for (int i = 0; i < array.length(); i++) {
Object object = array.get(i);
JSONObject jsonObject = (JSONObject) object;
//assuming current array member is object, call recursively with next key + index and current member
recursiveTraverse(nextKey + "-" + i, jsonObject);
//you might need to handle special case of current member being array
}
} else {
//value is neither object, nor array, so we print and this ends the recursion
System.out.println(nextKey + ", " + value);
}
}
}
Start traversing the object:
JSONObject jsonObject = new JSONObject(jsonString);
recursiveTraverse("", jsonObject);
It works good enough for most use cases, but as i have noted in comments in the code, it does not handle the case when current array element is another array. If your constraints allow this, you might have to implement recursive function for that . Something like this:
private static void recursiveHandleArray(String prevKey, JSONArray currentArray) {
//implement
}
Upvotes: 4