簡體   English   中英

使用嵌套 Json 進行 Object 映射的最佳方法

[英]Best Way to do Object Mapping with Nested Json

目前,我正在嘗試編寫一個使用 Feign 和 Spring 與公共 API 交互的站點。 我無法決定如何處理深度嵌套的 JSON 的 object 映射。

前任:

[
  {
    "type": "console",
    "category": "Console",
    "result_count": 1,
    "shown_count": 1,
    "result": [
      {
        "name": "Nintendo Switch",
        "id": "nintendo-switch",
        "platform": {
          "name": "Nintendo",
          "category": "nintendo",
          "type": "platform"
        },
        "image": {
          "url": "https://encrypted-tbn1.gstatic.com/shopping?q=tbn:ANd9GcRqJYIheMDjTE9WAHjMSW4bjh7OplS7Bep9CdsBBLWMwGdXim7xOG4&usqp=CAc",
          "height": 409,
          "width": 631
        },
        "min_price": 205,
        "variations": [
          {
            "items": [
              {
                "hex_code": "#696969",
                "name": "Gray",
                "id": "space-gray",
                "type": "color"
              },
              {
                "hex_code": "#C0C0C0",
                "name": "Silver",
                "id": "silver",
                "type": "color"
              }
            ],
            "name": "Color",
            "type": "color"
          },
          {
            "items": [
              {
                "name": "Nintendo",
                "id": "nintendo",
                "type": "platform"
              }
            ],
            "name": "Platform",
            "type": "platform"
          }
        ]
      }
    ]
  }
]

As of now, I have a single Java file with a class for each object in the JSON, and I've considered having the Object mapper just put everything into a HashMap. 有沒有更優雅的方法來做到這一點?

public class SearchResults {
    private List<SearchResult> products;
    private int resultCount;
    private String type;

}

class SearchResult {
    private String name;
    private String slug;
    private Image image;

}

class Image {
    private String URL;
    private String height;
    private String width;

}

基於提供的 json 文件,我設計了類,還提供了將 json 文件解析為 java 的代碼

public class  Console{
     String type;
     String category;
     int result_count;
     int show_count;
     Result [] result;
}

public class Result{
    String name;
    String id;
    Platform platform;
    Image image;
    int mini_price;
    Variation [] variations;
}

public class Platform{
    String name;
    String category;
    String type;
}

public class Image{
    String url;
    int height;
    int width;
}

public class Variation{
    String name;
    String type;
    Item [] items;

}

public class Item{
    String hex_code;
    String name;
    String id;
    String type;
}

要解析的代碼:

ObjectMapper objectMapper = new ObjectMapper();
         objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
         Console[] consoles = objectMapper.readValue(ResourceUtils.getFile("path of json file"), Console[].class);
         logger.info("Continents -> {}",(Object)continents);
         for(Console console:consoles) {
            //read the data accordingly
                     }

暫無
暫無

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

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