Spring 控制器:如何将 json 反序列化为 Map<Object, Object>
Spring controller : How to deserialize a json into a Map<Object, Object>
我使用 Unity 将 JSON 发送到 Spring 服务器。但是,我不知道如何反序列化这样一个复杂的对象。
最干净的方法是什么?
我有这 3 个 类 :
public class Garden {
private Integer width;
private Integer height;
private Map<Position, Sprite> objects;
public Garden(Integer width, Integer height, Map<Position, Sprite> objects) {
this.width = width;
this.height = height;
this.objects = objects;
}
/* getters and setters */
}
public class Position {
private int x;
private int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
/* getters and setters */
}
public class Sprite {
private String name;
private Boolean mirrored;
private Integer size;
public Sprite(String name, Boolean mirrored, Integer size) {
this.name = name;
this.mirrored = mirrored;
this.size = size;
}
/* getters and setters */
}
控制器内部的PostMapping方法:
@PostMapping
public ResponseEntity<Garden> postGarden(Garden garden) {
/* Doing things */
}
而JSON就是这种格式
{
"objects":{
"(1, 1)":{
"name":"Box",
"size":1,
"mirrored":false
},
"(5, 7)":{
"name":"Water",
"size":1,
"mirrored":false
}
},
"width":10,
"height":10
}
这里只有宽高传输成功,但是HashMap是空的
你知道怎么解决吗?
Json 的 objects
是 Map<String, Sprite>
而你的是 Map<Position, Sprite>
Json 解串器无法自动将 String
转换为 Position
。
您可以为您的 JSON 库提供 String->Position
的自定义转换器(请参阅您的库的文档)或者您可以 尝试 技巧 JSON图书馆
public class Garden {
private Integer width;
private Integer height;
private Map<Position, Sprite> objects;
public Garden(Integer width, Integer height, Map<Position, Sprite> objects) {
this.width = width;
this.height = height;
this.objects = objects;
}
public Map<Position, Sprite> getObjects() { return objects; }
public void setObjects(Map<Object, Sprite> objects) {
this.objects = new HashMap<>();
for (Map.Entry e : objects.entrySet()) {
Object key = e.getKey();
Sprite sprite = e.getValue();
if (key instanceof String) {
Position position = convertToPosition(key);
this.objects.put(position, sprite);
} else if (key instanceof Position){
this.objects.put((Position)key, sprite);
} else {
throw new IllegaStateException(key);
}
}
}
}
但是,最好是更改 Garden
对象结构
public class SpriteAtPosition {
private Position position;
private Sprite sprite;
...
}
public class Garden {
private Integer width;
private Integer height;
private List<SpriteAtPosition> sprites;
public Garden(Integer width, Integer height, List<SpriteAtPosition> sprites) {
this.width = width;
this.height = height;
this.sprites = sprites;
}
所以 Json 会是
{
"objects":[
{
"position": {
"x": 1,
"y": 1,
},
"sprite": {
"name":"Box",
"size":1,
"mirrored":false
}
},
{
"position": {
"x": 5,
"y": 7,
},
"sprite": {
"name":"Water",
"size":1,
"mirrored":false
}
}
],
"width":10,
"height":10
}
我使用 Unity 将 JSON 发送到 Spring 服务器。但是,我不知道如何反序列化这样一个复杂的对象。 最干净的方法是什么?
我有这 3 个 类 :
public class Garden {
private Integer width;
private Integer height;
private Map<Position, Sprite> objects;
public Garden(Integer width, Integer height, Map<Position, Sprite> objects) {
this.width = width;
this.height = height;
this.objects = objects;
}
/* getters and setters */
}
public class Position {
private int x;
private int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
/* getters and setters */
}
public class Sprite {
private String name;
private Boolean mirrored;
private Integer size;
public Sprite(String name, Boolean mirrored, Integer size) {
this.name = name;
this.mirrored = mirrored;
this.size = size;
}
/* getters and setters */
}
控制器内部的PostMapping方法:
@PostMapping
public ResponseEntity<Garden> postGarden(Garden garden) {
/* Doing things */
}
而JSON就是这种格式
{
"objects":{
"(1, 1)":{
"name":"Box",
"size":1,
"mirrored":false
},
"(5, 7)":{
"name":"Water",
"size":1,
"mirrored":false
}
},
"width":10,
"height":10
}
这里只有宽高传输成功,但是HashMap是空的
你知道怎么解决吗?
Json 的 objects
是 Map<String, Sprite>
而你的是 Map<Position, Sprite>
Json 解串器无法自动将 String
转换为 Position
。
您可以为您的 JSON 库提供 String->Position
的自定义转换器(请参阅您的库的文档)或者您可以 尝试 技巧 JSON图书馆
public class Garden {
private Integer width;
private Integer height;
private Map<Position, Sprite> objects;
public Garden(Integer width, Integer height, Map<Position, Sprite> objects) {
this.width = width;
this.height = height;
this.objects = objects;
}
public Map<Position, Sprite> getObjects() { return objects; }
public void setObjects(Map<Object, Sprite> objects) {
this.objects = new HashMap<>();
for (Map.Entry e : objects.entrySet()) {
Object key = e.getKey();
Sprite sprite = e.getValue();
if (key instanceof String) {
Position position = convertToPosition(key);
this.objects.put(position, sprite);
} else if (key instanceof Position){
this.objects.put((Position)key, sprite);
} else {
throw new IllegaStateException(key);
}
}
}
}
但是,最好是更改 Garden
对象结构
public class SpriteAtPosition {
private Position position;
private Sprite sprite;
...
}
public class Garden {
private Integer width;
private Integer height;
private List<SpriteAtPosition> sprites;
public Garden(Integer width, Integer height, List<SpriteAtPosition> sprites) {
this.width = width;
this.height = height;
this.sprites = sprites;
}
所以 Json 会是
{
"objects":[
{
"position": {
"x": 1,
"y": 1,
},
"sprite": {
"name":"Box",
"size":1,
"mirrored":false
}
},
{
"position": {
"x": 5,
"y": 7,
},
"sprite": {
"name":"Water",
"size":1,
"mirrored":false
}
}
],
"width":10,
"height":10
}