Parse a JSON Object With an Undetermined Amount of Child Nodes

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

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

回答1:

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;         }     } } 


回答2:

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             }         } 


回答3:

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



回答4:

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);  } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!