【发布时间】:2013-08-17 20:03:25
【问题描述】:
我在将 json 对象转换为 POJO 类时遇到问题。
我有一个名为 Service 的公共类,它有一个内部类 User。我想使用内部类作为容器/对象来保存我所有外部类方法的变量。我正在尝试执行以下操作,但出现编译错误。请展示我如何做到这一点,并请纠正我在下面的代码中所做的错误。
在 Eclipse 调试窗口中,我看到在节点变量中获得了以下 json 节点:{"firstName":"ndndbs","lastName":"dnjdnjs"}
试验一:
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 节点
-
嗨 Hanlet .. 我得到一个空指针异常
标签: java json jakarta-ee playframework jackson