Home > other >  How to access JSON field that doesn't have identifier
How to access JSON field that doesn't have identifier

Time:10-18

How could I access the #19 that is at the end of this JSON? Im using java 11 with GSON library but any library is okay or explanation.

Some context: usually I reference the field like .get("id") but that 19 is kind of the first time I see that and I know it is because of the circular reference that is in that JSON, we are using @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="id") Annotation on our model classes to fix it. Also to mention the field "functionalityList" are the list of childs of a functionality, so in this example functionality with id 15 has the child with id 19. I appreciate any help or advise

[
    {
        "id": 15,
        "name": "Imprimir",
        "description": "Funcionalidad que permite imprimir",
        "functionalityList": [
            {
                "id": 19,
                "name": "Imprimir PDF",
                "description": "Funcionalidad que permite imprimir PDF",
                "functionalityList": [],
                "parentid": 15
            }
        ],
        "parentid": null
    },
    19
]

CodePudding user response:

This is a JSON Array, elements in an array are not supposed to have an identifier, something like

JsonArray arr = new JsonParser().parse(jsonString).getAsJsonArray();

you can then get the object (the first item in the array)

JsonObject object = arr.get(0).getAsJsonObject();

or the in at the int (the second item)

int id = arr.get(1).getAsInt();
  • Related