使用未确定数量的子节点解析JSON对象 [英] Parse a JSON Object With an Undetermined Amount of Child Nodes
问题描述
我使用Dropbox API,我想从json对象获取文件夹和路径。但是我遇到了问题,因为有一些子节点被破坏了。我如何解析这样的东西?
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?
编辑
JSONObject代表一个目录。顶级文件夹由json对象表示。在该对象内是子文件夹。他们每个人都有JSONobject,在每个jsonobject中都有一个数组,代表子文件夹的子文件夹。所以我不知道json的完整结构
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
推荐答案
我刚才遇到了同样的问题,我写了一个类一个递归算法,通过并搜索特定的ID ..似乎工作正常,随时试一试!
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;
}
}
}
这篇关于使用未确定数量的子节点解析JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!