简体   繁体   English

如何将 JSON (org.jooq.JSON) 转换为 Java Object (POJO)

[英]How to convert JSON (org.jooq.JSON) to Java Object (POJO)

I have tried lot of ways to find to convert JSON object (org.jooq) to java object (pojo).我已经尝试了很多方法来找到将 JSON object (org.jooq) 转换为 java ZA8CFDE63311C59EB62AC. I am working on spring-boot application using jOOQ to talk with the database.我正在使用 jOOQ 与数据库对话的 spring-boot 应用程序。

Actually I have a employee_details table in database shown in below:实际上我在数据库中有一个employee_details 表,如下所示:

在此处输入图像描述

In the above table has employee details, In that table emp_address column type is JSON datatype上表中有员工详细信息,该表中 emp_address 列类型为 JSON 数据类型

Now I want to retrieve the one employee_details from table where id=X and I tried to store the details in below java object class (POJO).现在我想从 id=X 的表中检索一个employee_details,并尝试将详细信息存储在 java object class (POJO) 下面。

EmployeeDetails class:员工详情 class:

@AllArgsConstructor
@NoArgsConstructor
@Data
public class EmployeeDetails {
    private int              id;
    private String           emp_name;
    private String           emp_email;
    private String           emp_mobile;
    private EmployeeAddress  emp_address;
}

EmployeeAddress class:员工地址 class:

@AllArgsConstructor
@NoArgsConstructor
@Data
public class EmployeeAddress {

    private String hNo;
    private String city;
    private String pincode;
}

But when I try to retrieve the details from table I got store the emp_address(which is json datatype in table) in Json(org.jooq) as shown in below class.但是当我尝试从表中检索详细信息时,我将 emp_address(表中的 json 数据类型)存储在 Json(org.jooq)中,如下面的 class 所示。

EmployeeDetailsJSONJooQ class: EmployeeDetailsJSONJooQ class:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jooq.JSON;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class EmployeeDetailsJSONJooQ {
    private  int        id;
    private  String     emp_name;
    private  String     emp_email;
    private  String     emp_mobile;
    private  JSON       emp_address;
}

Here I want to retrieve the employee details in either EmployeeDetails object or help me to convert the EmployeeDetails object to EmployeeDetailsJSONJooQ object or how to map these objects using Mapstruct . Here I want to retrieve the employee details in either EmployeeDetails object or help me to convert the EmployeeDetails object to EmployeeDetailsJSONJooQ object or how to map these objects using Mapstruct . Please help me out请帮帮我

If you're not too opinionated on how the JSON should be mapped to your POJOs, it should suffice to place either Gson or Jackson on your classpath, and jOOQ will pick that up and map JSON to your POJOs automatically, see this section of the manual about the ConverterProvider . If you're not too opinionated on how the JSON should be mapped to your POJOs, it should suffice to place either Gson or Jackson on your classpath, and jOOQ will pick that up and map JSON to your POJOs automatically, see this section of the关于ConverterProvider的手册

Eg this should work out of the box:例如,这应该开箱即用:

List<EmployeeDetails> list =
ctx.selectFrom(EMPLOYEE_DETAILS)
   .fetchInto(EmployeeDetails.class);

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

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