使用GSON反序列化内部json字符串属性

rn0zuynd  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(166)

我使用Springboot和Gson作为我的后端。我有这两个多对多关系的类:

订单类

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table( name = "orders")
public class Order {
    @Id
    @Expose
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Expose
    @NotBlank
    private String poNumber;
    @Expose
    @NotBlank
    private String dimension;
    @Expose
    @NotBlank
    private int initialQuantity;
    @Expose
    @NotBlank
    private int leftQuantity;
    @Expose
    @NotBlank
    private Date startDate;
    @Expose
    @NotBlank
    private Date endDate;
    @Expose
    @SerializedName("status")
    @Enumerated(EnumType.STRING)
    @Column(length = 20)
    private PoStatus status;

    @OneToMany(mappedBy="order")
    private Set<Log> logs;

    @Expose
    @SerializedName("products")
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable( name = "orders_products",
            joinColumns = @JoinColumn(name = "order_id"),
            inverseJoinColumns = @JoinColumn(name = "sap_code"))
    private Set<Product> products = new HashSet<>();

产品类别

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table( name = "products")
public class Product {
    @Expose
    @Id
    @NotBlank
    private String sapCode;
    @Expose
    @NotBlank
    private String sapCodeDescription;
    @Expose
    private String productData;
}

这是我用来向我的rest端点提供数据的服务

public String getAllOrders() {
    List<Order> allOrders = orderRepository.findAll();
    String allOrdersResult = gson.toJson(allOrders);
    return allOrdersResult;
}

这是回应:

[
   {
       "id": 1,
       "poNumber": "003100059361",
       "dimension": "INTBKGR",
       "initialQuantity": 200000,
       "leftQuantity": 200000,
       "startDate": "17/08/2022 00:00",
       "endDate": "17/08/2022 00:00",
       "status": "READY",
       "products": [
           {
               "sapCode": "000000000000416234",
               "sapCodeDescription": "1.STUFE 15X",
               "productData": "{\"pieces\": 85, \"mark\": true, \"description\": \"elementum pellentesque quisque porta volutpat erat quisque erat eros viverra eget congue eget\"}"
           }
       ]
   }
]

我的目标是反序列化/转义productData String属性。
我尝试过创建一个ProductData类并使用@JsonAdapter注解,但据我所知,此注解用于需要为反序列化给予自定义行为的情况,我的示例中的JSON字符串非常简单,不需要任何特定的逻辑支持。

xggvc2p6

xggvc2p61#

我认为您必须为它声明一个类,并将productData类型从string更改为该类。

jyztefdp

jyztefdp2#

我以这种方式解决了这个问题,我认为这不是一个好的方法,我希望有一个更自动的方法来解决这个问题。

产品类别

public class Product {
    @Expose
    @Id
    @NotBlank
    private String sapCode;
    @Expose
    @NotBlank
    private String sapCodeDescription;

    //THIS PROPERTY IS TO SAVE THE JSON IN THE DB
    @NotBlank
    @Column(columnDefinition="TEXT")
    @JsonProperty
    private String productDataColumn;

    //THIS PROPERTY IS TO EXPOSE THE DATA TO THE API
    @Transient
    @Expose
    private ProductData productData;

    public void createProductData() {
        this.productData = new Gson().fromJson(productDataColumn, ProductData.class);
    }
}

产品数据类

public class ProductData  {
  @Expose
  public int pieces;
  @Expose
  public boolean marcatura;
  @Expose
  public String description;
}

订购服务

public String getAllOrders() {
    List<Order> allOrders = orderRepository.findAll();
    for(Order o : allOrders){
        Product orderProduct = o.getProducts().stream().findFirst().get();
        orderProduct.createProductData();
    }
    String allOrdersResult = gson.toJson(allOrders);
    return allOrdersResult;
}

相关问题