最佳方法来处理带有嵌套 JSON 的对象映射。

huangapple go评论67阅读模式
英文:

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:

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

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&lt;SearchResult&gt; 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(&quot;path of json file&quot;), Console[].class);
		 logger.info(&quot;Continents -&gt; {}&quot;,(Object)continents);
		 for(Console console:consoles) {
			//read the data accordingly
					 }

huangapple
  • 本文由 发表于 2020年5月29日 12:39:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/62078844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定