英文:
Best Way to do Object Mapping with Nested Json
问题
public class SearchResults {
    private List<SearchResult> result;
    private int result_count;
    private String type;
}
class SearchResult {
    private String name;
    private String id;
    private Platform platform;
    private Image image;
    private float min_price;
    private List<Variation> variations;
}
class Platform {
    private String name;
    private String category;
    private String type;
}
class Image {
    private String url;
    private int height;
    private int width;
}
class Variation {
    private List<Item> items;
    private String name;
    private String type;
}
class Item {
    private String hex_code;
    private String name;
    private String id;
    private String type;
}
英文:
Currently I'm trying to write a site that interacts with a public API using Feign and Spring.
I'm having trouble deciding how to handle the object mapping for deeply nested JSON.
Ex:
[
  {
    "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. Is there a more elegant way to do this?
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;
}
答案1
得分: 1
根据提供的 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("json 文件路径"), Console[].class);
logger.info("Continents -> {}", (Object) consoles);
for (Console console : consoles) {
    // 根据需要读取数据
}
请注意,代码中的 "json 文件路径" 需要替换为实际的 JSON 文件路径。
英文:
Based on the json file provided i have designed the classes and also provided the code to parse the json file to 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;
}
code to parse:
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
					 }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论