-1

I have a json response

"data": { "students": [ { "id": 100, "name": "ABC" }, { "id": 101, "name": "XYZ" }

I need to map it to my pojo, something like -

public class TempClass {

     List<Temp> list_students;

}

class Temp {
    Long id;
    String name;

}

Direct reading API response into my pojo gives me a class cast exception. I've tried converting response to a list of map and the collect as Temp class but that also doesn't work.

Exception -

java.util.LinkedHashMap cannot be cast to java object

Any suggestions please?

Code snippet for conversion -

new TempClass(((LinkedHashMap<String, Object>) response.getData()).entrySet())
                            .stream().map(map -> mapper.convertValue(map, Temp.class))
                            .collect(Collectors.toList()))
N.Rajal
  • 105
  • 2
  • 17
  • Also share the code of how you are converting the response into POJO. – Sayan Bhattacharya Jul 12 '22 at 09:12
  • Hey @SayanBhattacharya, updated. Please check. – N.Rajal Jul 12 '22 at 09:18
  • You are not considering`data` and `students` key while converting . If you are trying to convert the whole response then your POJO should also have all the properties. Its unnecessary to cast it into `HashMap`. You can check out [this thread](https://stackoverflow.com/questions/19271065/) – Sayan Bhattacharya Jul 12 '22 at 09:52

1 Answers1

2
public class Data{
    public ArrayList<Student> students;
}

public class Root{
    public Data data;
}

public class Student{
    public int id;
    public String name;
}

Your POJO class will look like this

Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31