繁体   English   中英

Spring controller:如何将 json 反序列化为 Z46F3EA056CAA31268CZZ0BEEA0<object, object></object,>

[英]Spring controller : How to deserialize a json into a Map<Object, Object>

我使用 Unity 将 JSON 发送到 Spring 服务器。 但是,我不知道如何反序列化如此复杂的 object。 最干净的方法是什么?

我有这 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 */
}

controller里面的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 的objectsMap<String, Sprite>而你的对象是Map<Position, Sprite>

Json 解串器无法自动将String转换为Position

您可以为您提供 String- String->Position的自定义转换器 JSON 库(请参阅您的库的文档),或者您可以尝试技巧 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 object 结构

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM