我正面临着将json对象转换为POJO类的问题。在外部类中使用内部类对象java
我有一个名为Service的公共类,它有一个内部类的用户。我想使用内部类作为容器/对象来容纳我所有外部类方法的变量。我正在尝试执行下面的操作,但是我收到了编译错误。请说明我如何做到这一点,并请纠正我在我的代码中所做的错误。
从蚀调试窗口中,我看到在节点变量 节点而获得的下面的JSON:{ “名字”: “ndndbs”, “姓氏”: “dnjdnjs”}
试验1:
public class Service {
// Method
public boolean createUserAccount(JsonNode node) throws Exception {
ObjectMapper mapper = new ObjectMapper();
User user=null;
try {
Service service=new Service();
user = mapper.readValue(node, User.class);
} catch (Exception e) {throw new Exception("failed to bind json", e);}
System.out.println("Use this anywhere in method"+userNode.firstName);
}
}
// Inner class
public class User {
public String firstName;
public String lastName;
}
}
OUTPUT:
NULL POINTER EXCEPTION AND user=null even after execution of mapper.readValue() statement
向我们展示JsonNode的外观(json以及如何初始化节点)。 –
得到编译错误或空对象异常? –
嗨Sotirios ...我编辑并在我的问题中添加了Json节点 – pret