to-java对象中的数值数组

jc3wubiy  于 2021-06-26  发布在  Java
关注(0)|答案(3)|浏览(354)

我喜欢用jsonb将json数据Map到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数组反序列化为类java.awt.point” List<Point2D> -->“无法将json数组反序列化为类java.awt.point2d”

lnvxswe2

lnvxswe21#

要找出默认Map,请使用非泛型字段并使用调试器进行观察:

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() {
    }
  }
}

9fkzdhlc

9fkzdhlc2#

因为我已经这么做了:更改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() { }
  }
}
5lwkijsr

5lwkijsr3#

让我们试试:

@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);
    }
}

相关问题