简体   繁体   English

浏览JSONObject Android

[英]Navigating through JSONObject Android

I am trying to implement a method which is able to navigate through a JSONObject until it finds a given parameter. 我正在尝试实现一种方法,该方法能够浏览JSONObject直到找到给定参数。 My JSON is structured like a foldersystem. 我的JSON的结构类似于文件夹系统。 There are folders and every folder can have files.A folder can have another folder and so on.JSON is looking like this: 有文件夹,每个文件夹可以有文件,一个文件夹可以有另一个文件夹,依此类推.JSON如下所示:

{
  "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". 因此,例如,我想编辑路径为“ / RootFolder / FolderInRoot / Bild2”的“ Bild2”。 Has someone a method which navigates to that given position. 有人可以导航到该给定位置的方法。 The path should be the parameter. 路径应该是参数。

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 (ie Bild2). 您需要遍历JSON响应,直到达到正确的深度为止,然后可以使用getString(“ name”)来获取该键(即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? 有关更多信息,请参见此StackOverflow问题: Json数组上的Java循环?

You also might want to consider using the Gson library to make your parsing a lot easier. 您可能还需要考虑使用Gson库来简化解析。 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). 如果您能够将Gson添加到项目中(我建议您这样做),那么会有大量很棒的教程来解析数据并将JSON反序列化为纯Java对象(这将避免嵌套循环的麻烦)。 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. 首先,您可以使用Android中的GSON解析复杂的JSON对象http://blog.nkdroidsolutions.com/how-to-parsing-json-array-using-gson-in-android-tutorial/

(PS you might want to consider simplifying your JSON structure if possible because it will be a lot easier to work with). (PS,如果可能的话,您可能要考虑简化JSON结构,因为使用起来会容易得多)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM