Skip to content

Instantly share code, notes, and snippets.

@ZoeyYoung
Created October 29, 2014 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZoeyYoung/4e4a91c19bf7f6de0a9c to your computer and use it in GitHub Desktop.
Save ZoeyYoung/4e4a91c19bf7f6de0a9c to your computer and use it in GitHub Desktop.
Java从Json文件中读取数据并封装成对象
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* 数据库驱动类, 从JSON文件<b>databaseDriver.json</b>中读取数据并封装为对象.<br>
*
* JSON文件格式如下:<br>
*
* <pre>
* {"db": [
* {
* "name": "PostgreSQL",
* "driver": "org.postgresql.Driver",
* "connectStr": "jdbc:postgresql://"
* },
* {
* "name": "MySQL",
* "driver": "com.mysql.jdbc.Driver",
* "connectStr": "jdbc:mysql://"
* }
* ]
* }
* </pre>
*
* @author yangdm.fnst
*
*/
public class DatabaseDriver {
/**
* Database Name, <b>e.g.</b> MySQL.
*/
private String name;
/**
* JDBC Database Driver, <b>e.g.</b> com.mysql.jdbc.Driver.
*/
private String driver;
/**
* JDBC Connect String, <b>e.g.</b> jdbc:mysql://.
*/
private String connectStr;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the driver
*/
public String getDriver() {
return driver;
}
/**
* @param driver the driver to set
*/
public void setDriver(String driver) {
this.driver = driver;
}
/**
* @return the connectStr
*/
public String getConnectStr() {
return connectStr;
}
/**
* @param connectStr the connectStr to set
*/
public void setConnectStr(String connectStr) {
this.connectStr = connectStr;
}
/**
* 获取databaseDriver.json中配置JDBC数据库信息,封装成DatabaseDriver对象.
*
* @return DatabaseDriver对象数据库名称和对象的Map
*/
public static Map<String, DatabaseDriver> getMap() {
// 获取json文件
File file = new File("databaseDriver.json");
Map<String, DatabaseDriver> map = new HashMap<String, DatabaseDriver>();
try {
String json = FileUtils.readFileToString(file);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(json).getAsJsonObject();
// 将db节点下的内容转为JsonArray
JsonArray jsonArray = jsonObject.getAsJsonArray("db");
for (int i = 0; i < jsonArray.size(); i++) {
JsonElement el = jsonArray.get(i);
// 映射为类实例
DatabaseDriver data = gson.fromJson(el, DatabaseDriver.class);
map.put(data.getName(), data);
}
} catch (IOException e) {
}
return map;
}
}
{"db": [
{
"name": "PostgreSQL",
"driver": "org.postgresql.Driver",
"connectStr": "jdbc:postgresql://"
},
{
"name": "MySQL",
"driver": "com.mysql.jdbc.Driver",
"connectStr": "jdbc:mysql://"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment