'How to get a list from a JsonArray of objects

I would like to convert the response.getBody of above call to array.

I am trying to parse only the array "data" of json as list.

JSON: 
{
    "totalValue": 21,
    "data": [
        {
            "id": 1,
            "firstname": "Tom",
            "lastname":"Pit"
        },
        {
              "id": 2,
            "firstname": "Jim",
            "lastname":"Sol"
        }
              
    ]
}

So after some tries i reach here:

 JSONParser parser = new JSONParser();
    Object obj  = (Object) parser.parse(response.getBody());
    JSONArray array = new JSONArray();
    array.add(obj);

This array has size: 1 in the array there is a json object with 2 values first is long value of the total value (21) second is JsonArray with value : all the values and key "data" .

I would like to parse the JsonArray as list of object in java...but whatever to try get the error most of the times..... Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column

Any help?



Solution 1:[1]

If you want to parse the "data" as list of object in java you can try with:

       Map<?, ?> mapResponse = response.getBody();
       List<?> data = (List<?>)mapResponse.get("data");

I hope this help you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Isma