0

I am trying to implement a method which is able to navigate through a JSONObject until it finds a given parameter. My JSON is structured like a foldersystem. There are folders and every folder can have files.A folder can have another folder and so on.JSON is looking like this:

{
  "Elements": [
{
  "file": {
    "files": [
      {
        "id": "562dd7a2-9268-46b2-8963-b7a3b43e906e", 
        "modified": 1457608018166, 
        "name": "Bild1" 
      }, 
      {
        "id": "0efd76e7-730e-428a-96a4-95e04844070a", 
        "modified": 1457608018166, 
        "name": "Audio" 
      }, 
      {
        "files": [
          {
            "id": "562dd7a2-9268-46b2-8963-b7a3b43e906e", 
            "modified": 1457608018166, 
            "name": "Bild2" 
          }, 
          {
            "id": "562dd7a2-9268-46b2-8963-b7a3b43e906e", 
            "modified": 1457608018166, 
            "name": "Bild3" 
          }, 
          {
            "id": "562dd7a2-9268-46b2-8963-b7a3b43e906e", 
            "modified": 1457608018166, 
            "name": "Bild4" 
          }
        ], 
        "id": "562dd7a2-9268-46b2-8963-b7a3b43e906e", 
        "name": "FolderInRoot"
      }
    ], 
    "id": "562dd7a2-9268-46b2-8963-b7a3b43e906e", 
    "name": "RootFolder"
  }
}, 
{
  "file": {
    "files": [], 
    "id": "562dd7a2-9268-46b2-8963-b7a3b43e906e", 
    "name": "AnotherRootFolder"
  }
}
  ]
}

So for example I want to edit "Bild2" which has the path "/RootFolder/FolderInRoot/Bild2". Has someone a method which navigates to that given position. The path should be the parameter.

davidOhara
  • 1,008
  • 5
  • 17
  • 39

1 Answers1

2

You need to loop through the your JSON response until you get to the correct depth, and then you can use getString("name") to get the content at that key (i.e. Bild2). Once you get the content from the object/key you can add it to a List or Map to iterate over or do anything else you want with the data. See this StackOverflow question for more info: Java loop over Json array?

You also might want to consider using the Gson library to make your parsing a lot easier. If you are able to add Gson to the project (and I suggest that you do), there are a ton of great tutorials for parsing data and deserializing JSON into plain Java objects (which will save you the headache of nested loops). Using GSON in Android to parse a complex JSON object and http://blog.nkdroidsolutions.com/how-to-parsing-json-array-using-gson-in-android-tutorial/ are two good resources to begin with.

(P.S. you might want to consider simplifying your JSON structure if possible because it will be a lot easier to work with).

Community
  • 1
  • 1
Eric Bachhuber
  • 3,872
  • 2
  • 19
  • 26
  • Would you give me a hint how to flatten my Json?! – davidOhara Mar 11 '16 at 09:50
  • Well when I say flatten I just mean simplify the structure -- try to ditch as many layers of JSON as possible even if it means duplicating some data. If you can't then just do your best to make the Java parsing code as readable as possible. And like I said in the answer use Gson's JsonObject/JsonArray instead of plain Java's JSONObject/JSONArray if you are able to add the dependency. http://stackoverflow.com/questions/20379387/parsing-json-nested-array-with-gson-on-android and http://blog.nkdroidsolutions.com/how-to-parsing-json-array-using-gson-in-android-tutorial/ – Eric Bachhuber Mar 11 '16 at 10:06