繁体   English   中英

如何将 JsonObject 存储到 Postgresql 数据库?

[英]How can I Store JsonObject to Postgresql Database?

在这里,我使用 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 连接的Application.property

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

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

暂无
暂无

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

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