我正面臨着將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