且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Java将数据从JSON文件导入MySQL数据库

更新时间:2022-11-02 23:18:49

在我的项目中,我使用了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.