【问题标题】:How can I Store JsonObject to Postgresql Database?如何将 JsonObject 存储到 Postgresql 数据库?
【发布时间】:2021-12-11 19:50:48
【问题描述】:

在这里,我使用 JsonObject 从本地目录获取 JSON 数据,我该怎么做才能将此 JsonObject 存储到我的 Postgresql 数据库中?

示例数据:

{
  "emp_id":1017,
  "emp_name":"Nagesh",
  "emp_designation":"Manager",
  "department":"Java2Novice",
  "salary":30000,
  "direct_reports":["Nataraj G","Kalyan","Mahitha"],
  "address": {"street":"MG Road","city":"Bangalore"}
}

我有适当的 Application.property 用于 Postgresql 连接

spring.datasource.url=jdbc:postgresql://localhost:5432/springjson
spring.datasource.username=postgres
spring.datasource.password=secret
spring.jpa.show-sql=true

如何在数据库中存储

@SpringBootApplication
public class SpringNestedJsonToDbApplication {

public static void main(String[] args) {

        SpringApplication.run(SpringNestedJsonToDbApplication.class,args);

  File jsonInputFile = new File("D:/sample.json");

            InputStream is;

            try {

                is = new FileInputStream(jsonInputFile);
                

                JsonReader reader = Json.createReader(is);
                

                JsonObject empObj = reader.readObject();
                System.out.println(empObj);
                reader.close();
             
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

【问题讨论】:

标签: java json postgresql spring-boot


【解决方案1】:

使用 JSONObject 的 .toString() 方法获取文本并保存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多