2

我喜欢使用 Jsonb 将 JSON 数据映射到 Java 对象是多么容易,但我似乎偶然发现了一个没有充分记录的用例......

鉴于此 json 数据:

{
  "id": "test",
  "points": [
    [
      -24.787439346313477,
      5.5551919937133789
    ],
    [
      -23.788913726806641,
      6.7245755195617676
    ],
    [
      -22.257251739501953,
      7.2461895942687988
    ]
  ]
}

什么可以用作存储点值的对象类型?

import jakarta.json.bind.annotation.JsonbProperty;

public class Temp {

    @JsonbProperty("id")
    private String id;
        
    @JsonbProperty("points")
    private ??? points;

    // Getters-Setters
}

所以我可以创建临时对象:

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
    
Jsonb jsonb = JsonbBuilder.create();
Temp temp = jsonb.fromJson(jsonString, Temp.class);

到目前为止,我已经尝试了以下方法:

  • List<Point>--> “无法将 JSON 数组反序列化为:class java.awt.Point”
  • List<Point2D>--> “无法将 JSON 数组反序列化为:class java.awt.Point2D”
4

3 回答 3

2

让我们尝试一下:

@Data
public class Temp {
    @JsonbProperty("id")
    private String id;
    
    @JsonbProperty("points")
    private List<List<BigDecimal>> points;
    
    public static void main(String[] args) {
        String jsonString = "{\n" +
                            "  \"id\": \"test\",\n" +
                            "  \"points\": [\n" +
                            "    [\n" +
                            "      -24.787439346313477,\n" +
                            "      5.5551919937133789\n" +
                            "    ],\n" +
                            "    [\n" +
                            "      -23.788913726806641,\n" +
                            "      6.7245755195617676\n" +
                            "    ],\n" +
                            "    [\n" +
                            "      -22.257251739501953,\n" +
                            "      7.2461895942687988\n" +
                            "    ]\n" +
                            "  ]\n" +
                            "}";
        Jsonb jsonb = JsonbBuilder.create();
        Temp temp = jsonb.fromJson(jsonString, Temp.class);
        System.out.println(temp);
    }
}
于 2021-01-06T08:02:58.280 回答
1

要找出默认映射,请使用非通用字段并使用调试器观察它:

public class Test {
  public static void main(String[] args) {
    String json = "{\"id\":\"test\",\"points\":[[-24.787439346313477,5.555191993713379],[-23.78891372680664,6.724575519561768],[-22.257251739501953,7.246189594268799]]}";
    Temp temp = JsonbBuilder.create().fromJson(json, Temp.class);
    System.out.println(temp.points);
  }

  public static class Temp {
    public String id     = null;
    public List   points = null;

    public Temp() {
    }
  }
}

在此处输入图像描述

于 2021-01-06T08:18:36.010 回答
1

因为我已经这样做了:更改 json 格式将允许这样做:

public class Test {
  public static void main(String[] args) {
    String json = "{\"id\":\"test\",\"points\":[ {\"x\" : 1.0, \"y\" : 2.0 }, {\"x\" : 3.0, \"y\" : 4.0 } ] }";
    Temp temp = JsonbBuilder.create().fromJson(json, Temp.class);
    System.out.println(temp.points);
  }

  public static class Temp {
    public String      id     = null;
    public List<Point> points = null;
    public Temp() { }
  }

  public static class Point {
    public double x;
    public double y;
    public Point() { }
  }
}
于 2021-01-06T08:37:36.247 回答