1

I working with the Dropbox API and I would like to get the folders and paths from a json object. However I am having problems as there is an undermined number of child nodes. How would I go about parsing something like this?

EDIT

The JSONObject represents a directory. The top level folder is represented by a json object. Within that object are the sub-folders. Each of them has there down JSONobject, within each of these jsonobject there is an array which represent the subfolders of the subfolder. So I don't know the complete structure of the json

jiduvah
  • 5,108
  • 6
  • 39
  • 55
  • but its not possible with a standard for loop? – jiduvah Jul 23 '12 at 15:31
  • Look under the title "Related" in the column to the right. – Markus Jarderot Jul 23 '12 at 15:32
  • Markus I am aware of how to parse a normal json object. I have been looking through stackoverflow for a while and can't find anything – jiduvah Jul 23 '12 at 15:35
  • 2
    @jiduvah if you are downvoting a response that others provide you, pls show the courtesy of letting them know why. Also, any response here is much less vague than the question you have posed. – Sid Jul 23 '12 at 15:48
  • Because they are wrong. Sorry if it was vague. I tried to reword it a couple of time, I guess I will do it again.. I commented on one of the answers, I didn't think I would need to copy and paste it. I didn't do all the downvoting. – jiduvah Jul 23 '12 at 15:51
  • please also try to improve your acceptance. There are at least two answers which cover your original question. Don't expect someone to write your code for you. – Rafael T Jul 24 '12 at 07:20
  • @RafaelT I think it is only joel who answered correctly. I have yet to take a good look at it so I have not accept it. I never once asked somebody to write my code. – jiduvah Jul 24 '12 at 08:00

4 Answers4

2

I just had this very same problem, I wrote a class with a recursive algorithm that goes through and searches for a specific id.. seems to work alright, feel free to give it a try!

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonReference {
    Object data;

    public JsonReference(String buildFromString){
        try {
            data = new JSONObject(buildFromString);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(data == null){
            try {
                data = new JSONArray(buildFromString);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public Object getAbstractJsonObject(){
        return data;
    }

    public List<String> parseForData(String data,boolean removeDuplicates){
        Traverser valFinder = new Traverser(0);
        valFinder.setValues(data, removeDuplicates);
        valFinder.traverseForStringValue(this.data);
        return valFinder.foundInstances;
    }

    class Traverser{
        List<String> foundInstances = new ArrayList<String>();
        String value;
        boolean removeDuplicates;

        public Traverser(int type){

        }

        public void setValues(String value,boolean removeDuplicates){
            this.value = value;
            this.removeDuplicates = removeDuplicates;
        }

        public void traverseForStringValue(Object root){

            if(root == null){
                return;
            }
            else if(root instanceof JSONObject){
                JSONObject self = (JSONObject)root;

                //if the key exists in this object.. save it!
                if(self.has(value)){
                    try {
                        if(!removeDuplicates || notRepeat(self.getString(value)))
                            foundInstances.add(self.getString(value));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    return;
                }

                //otherwise, see if you can dive deeper..
                JSONArray names = self.names();
                for(int i=0;i<names.length();i++){
                    String temp = null;
                    try{
                        temp = names.getString(i);
                    }
                    catch(JSONException e){
                        e.printStackTrace();
                    }
                    if(temp != null){
                        try {
                            if(self.get(temp) instanceof JSONObject || self.get(temp) instanceof JSONArray)
                                traverseForStringValue(self.get(temp));
                            else if(self.get(temp) instanceof String){
                                if(!removeDuplicates || notRepeat(self.getString(value)))
                                    foundInstances.add(self.getString(value));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
            else if(root instanceof JSONArray){
                JSONArray self = (JSONArray)root;

                //iterate through the array..
                for(int i=0;i<self.length();i++){
                    Object temp = null;
                    try {
                        temp = self.get(i);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if(temp != null && temp != JSONObject.NULL){

                        if(temp instanceof JSONObject || temp instanceof JSONArray)
                            traverseForStringValue(temp);
                        else if(temp instanceof String && ((String)temp).contains(value)){
                            try {
                                if(!removeDuplicates || notRepeat(self.getString(i)))
                                    foundInstances.add(self.getString(i));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
            }
        }

        private boolean notRepeat(String s){
            for(String item : foundInstances){
                if(item.equals(s))
                    return false;
            }
            return true;
        }
    }
}
Joel
  • 4,732
  • 9
  • 39
  • 54
  • I think this is the best answer up until now, I will check and let you know how it goes – jiduvah Jul 23 '12 at 15:43
  • Yes, the other answers do not allow for arrays of arrays and more objects, i know how complex this can be. This class can represent a JSONArray or a JSONObject, the constructor figures it out for you so you dont have to worry about it. – Joel Jul 23 '12 at 15:45
1

Got this response from https://stackoverflow.com/a/10593838/259889

JSONObject jObject = new JSONObject(contents.trim());
   Iterator<?> keys = jObject.keys();

        while( keys.hasNext() ){
            String key = (String)keys.next();
            if( jObject.get(key) instanceof JSONObject ){
                // do your stuff
            }
        }
Community
  • 1
  • 1
Sid
  • 4,893
  • 14
  • 55
  • 110
0

for a JsonObject with nodes you can do this:

JsonObject jsonO = //get your JsonObject
List<String> keys = jsonO.keys();
List<Object> nodes = new ArrayList<Object>

for(String k: keys){
  nodes.add(jsonO.get(k));
}

if it is a JsonArray

JSONArray jsonA = //get Your JsonArray
List<Object> nodes = new ArrayList<Object>
for (int i = 0; i < jsonA.length(); i++){
  nodes.add(jsonA.get(i));
}

or consider using GSON

Rafael T
  • 15,401
  • 15
  • 83
  • 144
-2

It's probably a JSONArray you want to be converting it to. If you aren't pulling values based on their ids in a JSONObject, then it's a JSONArray. You can just do:

for (int i = 0; i < jsonArray.length(); i++) {

    whateverTheObjectIs = jsonArray.getObject(i);

}
leenephi
  • 900
  • 5
  • 13
  • No, I have a jsonarray, within that json array I a number of json objects. Within that json object I have an array and so on. It goes deeper and deeper. – jiduvah Jul 23 '12 at 15:39
  • This only allows for going through a single array, not at all what his question was asking. – Joel Jul 23 '12 at 15:47
  • Ah, gotcha. You weren't very clear on needing depth in parsing rather than just a way to iterate simply through JSON. – leenephi Jul 23 '12 at 15:48
  • Yeah I didn't catch that; sorry! – leenephi Jul 23 '12 at 15:49