簡體   English   中英

如何將Json以下轉換為Java Pojo class?

[英]how to convert below Json To Java Pojo class?

請在 Json 下方查看。 我想將其轉換為 POJO class。

我試過下面的 class

 @Data public class Root { @JsonProperty("0") // Dont want to pass like this. it should be single list and index is the key of that array public List<Request> list; @JsonProperty("1") public List<Request> list1; @JsonProperty("2") public List<Request> list2; @Data private class Request { int id; String title; int level; List<Request> children; @JsonProperty("parent_id") int parentId; } }

{
  "0": [
    {
      "id": 12123,
      "title": "sfsdf",
      "level": 0,
      "children": [
        
      ],
      "parent_id": null
    }
  ],
  "1": [
    {
      "id": 213,
      "title": "d",
      "level": 1,
      "children": [
        
      ],
      "parent_id": 1212312
    },
    {
      "id": 3232,
      "title": "dfsdf",
      "level": 1,
      "children": [
        
      ],
      "parent_id": 42
    },
    {
      "id": 234,
      "title": "tder",
      "level": 1,
      "children": [
        
      ],
      "parent_id": 122
    }
  ],
  "2": [
    {
      "id": 452,
      "title": "Blll",
      "level": 2,
      "children": [
        
      ],
      "parent_id": 322
    },
    {
      "id": 123,
      "title": "trrr",
      "level": 11,
      "children": [
        
      ],
      "parent_id": 1221
    },
    {
      "id": 33,
      "title": "sdfw",
      "level": 2123,
      "children": [
        
      ],
      "parent_id": 10
    }
  ]
}

使用@JsonAnySetter

示例,為簡單起見使用public字段:

class Root {
    @JsonAnySetter
    public Map<String, List<Request>> lists = new LinkedHashMap<>();
}

class Request {
    public int id;
    public String title;
    public int level;
    public List<Request> children;
    @JsonProperty("parent_id")
    public Integer parentId; // 'Integer', not 'int', since it can be null
}

測試

ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(new File("test.json"), Root.class);
for (Entry<String, List<Request>> entry : root.lists.entrySet()) {
    System.out.println(entry.getKey() + ":");
    for (Request request : entry.getValue()) {
        System.out.println("  id=" + request.id +
                        ", title=" + request.title +
                        ", level=" + request.level +
                     ", children=" + request.children +
                     ", parentId=" + request.parentId);
    }
}

Output

0:
  id=12123, title=sfsdf, level=0, children=[], parentId=null
1:
  id=213, title=d, level=1, children=[], parentId=1212312
  id=3232, title=dfsdf, level=1, children=[], parentId=42
  id=234, title=tder, level=1, children=[], parentId=122
2:
  id=452, title=Blll, level=2, children=[], parentId=322
  id=123, title=trrr, level=11, children=[], parentId=1221
  id=33, title=sdfw, level=2123, children=[], parentId=10

只需刪除第二個和第三個列表。 只保留單個列表,並讓它保留其名稱,如listrequests 然后添加到該列表中。

Root root = new Root(); // whatever
root.getRequests().add(myFirstRequest); // index 0 in the array
root.getRequests().add(mySecondRequest); // index 1 in the array
root.getRequests().add(myThirdRequest); // index 2 in the array

String json = // however you were making json out of Root before

通過在 controller 請求正文中傳遞Map<String, List<Request>> lists 我的問題已經解決了。 正如我所料,我得到了 JSON。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM