简体   繁体   中英

I don't know how to parse this nested json

I'm trying to parse this JSON using. However, I don't know how to parse "children". How can I map it to a class?

I created a class to parse "displayProperties" but I don't know how to parse "children"

{
  "displayProperties": {
    "description": "",
    "name": "Les Ténèbres",
    "icon": "/common/destiny2_content/icons/39ba3c78d0ad06c0728374d591c65821.png",
    "hasIcon": true
  },
  "originalIcon": "/common/destiny2_content/icons/39ba3c78d0ad06c0728374d591c65821.png",
  "rootViewIcon": "/common/destiny2_content/icons/39ba3c78d0ad06c0728374d591c65821.png",
  "nodeType": 1,
  "scope": 1,
  "objectiveHash": 4168198558,
  "children": {
    "presentationNodes": [
      {
        "presentationNodeHash": 655926402
      },
      {
        "presentationNodeHash": 2082711113
      },
      {
        "presentationNodeHash": 2474271317
      }
    ],
    "collectibles": [],
    "records": []
  }
}
abstract class DestinyObject(json : String) : JSONObject(json){

    fun optDisplayProperties() : DisplayProperties{
        val json_object : DisplayProperties = this.optJSONObject("displayProperties") as DisplayProperties
        return json_object
    }
}
class DisplayProperties(json : String) : JSONObject(json){
    val name = this.optString("name")
    val icon = this.optString("icon")
    val description = this.optString("description")

}

Create you Model class as below:

class Model(
    val displayProperties: DisplayProperties,
    val originalIcon: String,
    val rootViewIcon: String,
    val nodeType: String,
    val scope: String,
    val objectiveHash: Long,
    val children: Children
)

class Children(
    val presentationNodes: List<Any>,
    val collectibles: List<Any>,
    val records: List<Any>
)

class DisplayProperties(
    val description: String,
    val name: String,
    val icon: String,
    val hasIcon: Boolean
)

If you want to use any other model instead of Any , then you have to create this model also. Hope this will help you. In addition with this you may use JSON Parser library like GSON to parse the JsonObject. For the above JSON object the code looks like:

Gson().fromJson(json, Model::class.java)

Just filter every object alone on any Json parsing website and can use Gson().fromJson(json, Model::class.java) in your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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