Jsonnode.get (string) returning nullJAVA

Программисты JAVA общаются здесь
Anonymous
Jsonnode.get (string) returning null

Сообщение Anonymous »

Я пытаюсь создать Pojo из файла JSON. Но по какой -то причине "node.get (" name "). Astext ()" возвращает NULL. < /P>
Ошибка происходит в строке 8, и вот что бросает отладчик. "com.fasterxml.jackson.databind.jsonnode.get (string)" is null. < /p>
Я уже пробовал "node.get (" solution "). get (" name "). Astext ()" и переключение "astext () на" toString () ", результат был тем же.

Код: Выделить всё

public static solution findSolution(String name){
File file = new File("/home/clementino/IdeaProjects/helloworldJava/problems.json");
ObjectMapper mapper = new ObjectMapper();
solution solution = new solution();
try {
JsonNode tree = mapper.readTree(file);
for(JsonNode node : tree){
if(node.get("name").asText().equalsIgnoreCase(name)){ //problema
solution = mapper.treeToValue(node, solution.class);
System.out.println(solution.getName());
}else{
System.out.println("problem doesnt exist");
}
}
}catch(Exception e){
System.out.println("error");
}
return solution;
}
< /code>
problems.json:


{
"solution": [
{
"name":"twoSum",
"difficulty": "easy",
"status": "time exceeded",
"lastModified": "12-05-2025",
"testCase": false,
"source": "leetcode.com",
"language": "java"
},
{
"name": "Watermelon",
"difficulty": "easy",
"status": "working",
"lastModified": "05-02-2024",
"testCase": false,
"source": "codeforces.com",
"language": "C++"

}
]
}



Подробнее здесь: https://stackoverflow.com/questions/796 ... rning-null

Вернуться в «JAVA»