gpt4 book ai didi

java - 将具有动态键的嵌套对象映射到 Java 对象

转载 作者:行者123 更新时间:2023-11-30 05:45:04 27 4
gpt4 key购买 nike

我遇到过这样的情况:我试图将 JSON 映射到 Java 对象,该对象由带有动态键的嵌套对象组成。我尝试将其映射到 LinkedHashMap 但失败了。

下面是我尝试映射的 JSON 输出:

{
"success": true,
"item": {
"id": 8903,
"firstLevel": {
"266940": {
"SecondLevel": {
"407617": {
"thirdLevel": {
"2381098": {
"outcome": "PASS",
"screenshot": "2617.png",
"dataRow": 0
},
"2381134": {
"outcome": "PASS",
"screenshot": "06d.png",
"dataRow": 0
}
}
},
"407620": {
"thirdLevel": {
"2381043": {
"outcome": "PASS",
"screenshot": "2617.png",
"dataRow": 0
},
"2381198": {
"outcome": "PASS",
"screenshot": "06d.png",
"dataRow": 0
}
}
}
}
} ,
"266941": {
"SecondLevel": {
"407617": {
"thirdLevel": {
"2381094": {
"outcome": "PASS",
"screenshot": "2617.png",
"dataRow": 0
},
"2381138": {
"outcome": "PASS",
"screenshot": "06d.png",
"dataRow": 0
}
}
},
"407620": {
"thirdLevel": {
"2381047": {
"outcome": "PASS",
"screenshot": "2617.png",
"dataRow": 0
},
"2381191": {
"outcome": "PASS",
"screenshot": "06d.png",
"dataRow": 0
}
}
}
}
}
},
"outcome": "PASS",
"status": "FINISHED"
}
}

我还使用@JsonAnySetter将动态值映射到LinkedHashMap,但这也没有帮助。

 private Map<String, FirstLevel> firstLevel = new LinkedHashMap<>();

public Map<String, FirstLevel> getFirstLevel() {
return firstLevel;
}

public void setFirstLevel(Map<String, FirstLevel> firstLevel) {
this.firstLevel = firstLevel;
}

@JsonAnySetter
public void setFirstLevel(String key, FirstLevel value) {
this.firstLevel.put(key, value);
}

下面是第一层对象的代码

  private Map<String, FirstLevel> firstLevel = new LinkedHashMap<>();

public Map<String, FirstLevel> getFirstLevel() {
return firstLevel;
}

public void setFirstLevel(Map<String, FirstLevel> firstLevel) {
this.firstLevel = firstLevel;
}

最佳答案

您需要在第一级使用@JsonAnySetter,并在每个下一级使用@JsonCreator。您可以在下面找到完整的工作解决方案。由于每个级别都有包装对象,因此有点复杂。要跳过它们,我们可以编写自定义反序列化器或在构造函数中使用JsonCreator:

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.Map;

public class JsonApp {

public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();

ObjectMapper mapper = new ObjectMapper();
Response response = mapper.readValue(jsonFile, Response.class);

response.getItem().getNextLevel().forEach((k, v) -> {
System.out.println(k);
v.getNextLevel().forEach((k1, v1) -> {
System.out.println("\t" + k1);
v1.getNextLevel().forEach((k2, v2) -> {
System.out.println("\t\t" + k2 + " => " + v2);
});
});
});
}
}

class Response {

private boolean success;
private Item item;

public boolean isSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

public Item getItem() {
return item;
}

public void setItem(Item item) {
this.item = item;
}

@Override
public String toString() {
return "Response{" +
"success=" + success +
", item=" + item +
'}';
}

public static <T> Map<String, T> getOne(Map<String, Map<String, T>> map) {
if (map == null || map.size() == 0) {
return null;
}

return map.values().iterator().next();
}
}

class Item {

private int id;
private String outcome;
private String status;
private Map<String, SecondLevel> nextLevel;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getOutcome() {
return outcome;
}

public void setOutcome(String outcome) {
this.outcome = outcome;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Map<String, SecondLevel> getNextLevel() {
return nextLevel;
}

@JsonAnySetter
public void set(String property, Map<String, SecondLevel> map) {
nextLevel = map;
}

@Override
public String toString() {
return "Item{" +
"id=" + id +
", outcome='" + outcome + '\'' +
", status='" + status + '\'' +
", firstLevel=" + nextLevel +
'}';
}
}

class SecondLevel {

private Map<String, OutcomeLevel> nextLevel;

@JsonCreator
public SecondLevel(Map<String, Map<String, OutcomeLevel>> map) {
this.nextLevel = Response.getOne(map);
}

public Map<String, OutcomeLevel> getNextLevel() {
return nextLevel;
}

@Override
public String toString() {
return "SecondLevel{" +
"nextLevel=" + nextLevel +
'}';
}
}

class OutcomeLevel {

private Map<String, Outcome> nextLevel;

@JsonCreator
public OutcomeLevel(Map<String, Map<String, Outcome>> map) {
this.nextLevel = Response.getOne(map);
}

public Map<String, Outcome> getNextLevel() {
return nextLevel;
}

@Override
public String toString() {
return "OutcomeLevel{" +
"nextLevel=" + nextLevel +
'}';
}
}

class Outcome {

private String outcome;
private String screenshot;
private int dataRow;

public String getOutcome() {
return outcome;
}

public void setOutcome(String outcome) {
this.outcome = outcome;
}

public String getScreenshot() {
return screenshot;
}

public void setScreenshot(String screenshot) {
this.screenshot = screenshot;
}

public int getDataRow() {
return dataRow;
}

public void setDataRow(int dataRow) {
this.dataRow = dataRow;
}

@Override
public String toString() {
return "Outcome{" +
"outcome='" + outcome + '\'' +
", screenshot='" + screenshot + '\'' +
", dataRow=" + dataRow +
'}';
}
}

上面的代码打印:

266940
407617
2381098 => Outcome{outcome='PASS', screenshot='2617.png', dataRow=0}
2381134 => Outcome{outcome='PASS', screenshot='06d.png', dataRow=0}
407620
2381043 => Outcome{outcome='PASS', screenshot='2617.png', dataRow=0}
2381198 => Outcome{outcome='PASS', screenshot='06d.png', dataRow=0}
266941
407617
2381094 => Outcome{outcome='PASS', screenshot='2617.png', dataRow=0}
2381138 => Outcome{outcome='PASS', screenshot='06d.png', dataRow=0}
407620
2381047 => Outcome{outcome='PASS', screenshot='2617.png', dataRow=0}
2381191 => Outcome{outcome='PASS', screenshot='06d.png', dataRow=0}

另请参阅:

关于java - 将具有动态键的嵌套对象映射到 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55003810/

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