【问题标题】:How to parse array of numeric values in a JSON to Java object with Jsonb如何使用 Jsonb 将 JSON 中的数值数组解析为 Java 对象
【发布时间】:2021-01-06 07:26:22
【问题描述】:

我喜欢使用 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 数组反序列化为:java.awt.Point 类"
  • List<Point2D> --> "无法将 JSON 数组反序列化为:java.awt.Point2D 类"

【问题讨论】:

  • Temp, No default constructor foundTemp 有默认构造函数吗?
  • @marc 这是我这边的错误测试用例,所以从问题中删除

标签: java json jsonb yasson


【解决方案1】:

让我们试试吧:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 确实,BigDecimals 似乎是要走的路。您和@logbasex 的回答都帮了大忙,谢谢!
    • 如果控制 json 格式,可以将点列表更改为 [ { "x" : 1, "y": 2}, ...] 并将其映射到您自己的自定义 Point 类。
    • 确实,这是我的 B 计划,但需要检查是否可行
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      相关资源
      最近更新 更多