使用Java将数据从JSON文件导入MySQL数据库 [英] Importing data from JSON file to MySQL database using java

查看:1492
本文介绍了使用Java将数据从JSON文件导入MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到了将数据动态导入到MySQL数据库中的要求(但是通过使用java).

我不确定什么是最佳做法,但让我提出自己的方案.

json可以是动态的,并且不确定我每次从API端点读取时json的内容 例如:

"table1" : {
    "field1" : "value1",
    "field2" : "value2",
    "field3" : "value3"
}

一次json会是这样

其他时间可能是

"table2" : {
    "field1" : "value1",
    "field2" : "value2",
    "field3" : "value3",
    "field4" : "value4"
}

所以我的问题是我应该解析json并将其与与json名称匹配的表中的字段进行比较吗?

或者java中是否有任何可用的库可以使我轻松高效地处理此问题.

注意::我必须将其准备为一项服务,该服务将在一定间隔后连续扫描给定的API,并且需要将相应的JSON导入到它所属的表中,如果该表不存在,不存在,那么它必须自动创建.

感谢您的耐心阅读和支持,这是我从未处理过的一种新要求.

先谢谢您!

解决方案

在我的项目中,我使用了google-gson API.它工作得很好,而且很轻松. 您可以在此处找到描述.

以下是来自API docs 的简单示例,它支持多维数组,具有任意复杂的元素类型:

收藏集示例

Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);

// Serialization
String json = gson.toJson(ints);  // ==> json is [1,2,3,4,5]

// Deserialization
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
// ==> ints2 is same as ints

希望这会有所帮助.

I currently ran into a requirement of having the data dynamically imported into MySQL database (But by using java).

I am not sure what is the best practice for this but let me put forward my scenario.

The json can be dynamic and its uncertain of what will be in the json each time I read from an API endpoint for example:

"table1" : {
    "field1" : "value1",
    "field2" : "value2",
    "field3" : "value3"
}

One time the json will be like this

the other time it might be like

"table2" : {
    "field1" : "value1",
    "field2" : "value2",
    "field3" : "value3",
    "field4" : "value4"
}

so my question is should I parse the json and compare it with the fields in table which matches the json name?

or is there any library available in java which will enable me to easily and efficiently handle this.

NOTE: I have to prepare this as a service which will continously scan a given API after certain interval and the respective JSON needs to get imported into the table it belongs, and if the table doesn't exist then it must get automatically created.

Thanks all for your patient reading and support, this is a new kind of requirement which I have never handled.

Thank you in Advance!

解决方案

In my project i used google-gson API. It worked very well and it was painless. You can find the project with it's description here.

Below is a simple example from API docs wich supports multi-dimensional arrays, with arbitrarily complex element types:

Collections Examples

Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);

// Serialization
String json = gson.toJson(ints);  // ==> json is [1,2,3,4,5]

// Deserialization
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
// ==> ints2 is same as ints

Hope this helps.

这篇关于使用Java将数据从JSON文件导入MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆