【发布时间】: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