pom. xml
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>
класс pojo: Функция. java
public class Function{
private Integer id;
private Set<MenuToFunction> menuToFunctions;
public Integer getId(){
return this.id;
}
public void setId(Integer id){
this.id=id;
}
public Set<MenuToFunction> getMenuToFunctions(){
return this.menuToFunctions;
}
public void setMenuToFunctions(Set<MenuToFunction> menuToFunctions){
this.menuToFunctions=menuToFunctions;
}
}
класс pojo: MenuToFunction. java
public class MenuToFunction{
private Integer id;
public Integer getId(){
return this.id;
}
public void setId(Integer id){
this.id=id;
}
}
мой json файл, обратите внимание, что эти идентификаторы от 1 до 6.
{
"id": 1,
"menuToFunctions": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
},
{
"id": 4
},
{
"id": 5
},
{
"id": 6
}
]
}
и, наконец, мой тестовый код. Я использую Джексона и Гсона для десериализации json.
public class ReadJsonFile{
public static void main(String[] args){
try{
String fileAbsolutePath="T:/GetFunctionDetailResponse.json";
Function function=new ObjectMapper().readValue(new File(fileAbsolutePath),Function.class);
for(MenuToFunction menuToFunction:function.getMenuToFunctions()){
System.out.println("menuToFunction.getMenu().getId() with jackson:"+menuToFunction.getId());
}
function=new GsonBuilder().create().fromJson(new FileReader(fileAbsolutePath),Function.class);
for(MenuToFunction menuToFunction:function.getMenuToFunctions()){
System.out.println("menuToFunction.getMenu().getId() with gson:"+menuToFunction.getId());
}
}catch(Exception e){
e.printStackTrace();
}
}
}
консольный вывод
menuToFunction.getMenu().getId() with jackson:5
menuToFunction.getMenu().getId() with jackson:3
menuToFunction.getMenu().getId() with jackson:6
menuToFunction.getMenu().getId() with jackson:2
menuToFunction.getMenu().getId() with jackson:4
menuToFunction.getMenu().getId() with jackson:1
menuToFunction.getMenu().getId() with gson:1
menuToFunction.getMenu().getId() with gson:2
menuToFunction.getMenu().getId() with gson:3
menuToFunction.getMenu().getId() with gson:4
menuToFunction.getMenu().getId() with gson:5
menuToFunction.getMenu().getId() with gson:6
Так почему Джексону десериализовать json не удалось напечатать массив объектов в последовательности, , но gson может ?
Есть ли конфигурация для Джексона?
Я также попробовал последнюю версию jackson-2.10.3, но все еще имею ту же проблему.
Спасибо за любую помощь.