I have json file
{"q":{"name": "Alex"}}
Class People
private String name;
I try to pars json into LinkedHashMap<String, People>.
public class Parser {
public Root read(){
LinkedHashMap<String, People> w=new ObjectMapper().readValue("{\"q\":{\"name\":\"Alex\"} }",LinkedHashMap.class);
Root root=new Root();
root.setMap(w);
return root;
}
}
//Class Root has private LinkedHashMap<String, People> map;
I try to cast root.getMap().get("q"). It's working ok, but when I cast root.getMap().get("q").getClass():
Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class Collect.Root (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; Collect.Root is in unnamed module of loader 'app')
I tried to put another object in Linkedhashmap root and compare them in debugging
People test=new People();
test.name="qqq";
Parser pars = new Parser();
Root root = pars.read();
root.put("w",test);
As I understand it, the Parser does not understand that there is an object of type People in {...}
System.out.println(w.get("q")+" | "+w.get("w"));
I do not know how else to simplify the code, because if I remove something else, the error disappears