使用动态键将嵌套对象映射到Java对象

时间:2019-03-05 13:18:24

标签: java json jackson gson

我遇到一种情况,我试图将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);
 }

下面是firstLevel对象的代码

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

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

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

1 个答案:

答案 0 :(得分:1)

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

constructor

上面的代码显示:

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 +
                '}';
    }
}

另请参阅: