gpt4 book ai didi

java - 为什么 Jackson 反序列化 json 无法按顺序打印对象数组?

转载 作者:行者123 更新时间:2023-12-01 17:50:23 24 4
gpt4 key购买 nike

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类:Function.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 文件,请注意这些 id 是从 1 到 6。

{
"id": 1,
"menuToFunctions": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
},
{
"id": 4
},
{
"id": 5
},
{
"id": 6
}
]
}

最后是我的测试代码。我使用 jackson 和 gson 来反序列化 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

那么为什么 Jackson 反序列化 json 无法按顺序打印对象数组,但 gson 可以

Jackson 有配置吗?

我也尝试过最新版本jackson-2.10.3,但仍然有同样的问题。

感谢您的帮助。

最佳答案

有一个属性可以提供帮助:(来自 jackson 的documentation.)

@JsonPropertyOrder({ "id", "menuToFunctions" })
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;
}
}

还包括在 ObjectMapper 中排序:

public class ReadJsonFile{

public static void main(String[] args){

try{
String fileAbsolutePath="T:/GetFunctionDetailResponse.json";

Function function=new ObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.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();
}
}
}

关于java - 为什么 Jackson 反序列化 json 无法按顺序打印对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60807621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com