gpt4 book ai didi

java - 使用数据库中的数据创建 JSON 数组

转载 作者:行者123 更新时间:2023-12-02 06:22:37 26 4
gpt4 key购买 nike

我的数据库 rel_act 中的特定字段的值存储为 1234,5678,9112我通过映射数组“rslt”读取这些值,因此当我将它们读取为

我希望以特定格式创建一个 json 对象

“rel_act”:[{“ref”:“1234”},{“ref”:“5678”},{“ref”:“9112”}]

请指教

谢谢

最佳答案

您可以对 JDBC 代码使用一个简单的包装器:(请注意,本示例中未包含 SQL2JSON.convertDateToString() 方法,请将其删除或添加您自己的方法)

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Creates a JSONARRAY from an java.sql.ResultSet.
* @author Aries
*
*/
public class SQL2JSON
{

public static JSONArray convert(ResultSet rs) throws SQLException, JSONException
{
JSONArray json = new JSONArray();
ResultSetMetaData rsmd = rs.getMetaData();
rs.beforeFirst();
while(rs.next()) {
int numColumns = rsmd.getColumnCount();
JSONObject obj = new JSONObject();

for(int i=1; i<numColumns+1; i++) {

String column_name = rsmd.getColumnLabel(i); //Bugfix , works better than getColumnName() /Aries

switch( rsmd.getColumnType( i ) ) {
case java.sql.Types.ARRAY:
obj.put(column_name, rs.getArray(column_name)); break;
case java.sql.Types.BIGINT:
obj.put(column_name, rs.getInt(column_name)); break;
case java.sql.Types.BOOLEAN:
obj.put(column_name, rs.getBoolean(column_name)); break;
case java.sql.Types.BLOB:
obj.put(column_name, rs.getBlob(column_name)); break;
case java.sql.Types.DOUBLE:
obj.put(column_name, rs.getDouble(column_name)); break;
case java.sql.Types.FLOAT:
obj.put(column_name, rs.getFloat(column_name)); break;
case java.sql.Types.INTEGER:
obj.put(column_name, rs.getInt(column_name)); break;
case java.sql.Types.NVARCHAR:
obj.put(column_name, rs.getNString(column_name)); break;
case java.sql.Types.VARCHAR:
obj.put(column_name, rs.getString(column_name)); break;
case java.sql.Types.TINYINT:
obj.put(column_name, rs.getInt(column_name)); break;
case java.sql.Types.SMALLINT:
obj.put(column_name, rs.getInt(column_name)); break;
case java.sql.Types.DATE:
obj.put(column_name, SQL2JSON.convertDateToString(rs.getDate(column_name))); break;
case java.sql.Types.TIMESTAMP:
obj.put(column_name, SQL2JSON.convertDateToString(rs.getTimestamp(column_name))); break;
default:
obj.put(column_name, rs.getObject(column_name)); break;
}
}

json.put(obj);
}

return json;
}
}

这是一个如何调用它的示例。我正在使用自定义 MySQL 事务池和 JDBC SQL 句柄对象,但它应该可以修改为任何 JDBC 驱动程序对象(MySQL、Sybase、H2、HSQLDB、Oracle...)

import java.sql.ResultSet;
import java.sql.SQLException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public synchronized JSONObject getCustomerReferences(int custID) {

DBTransaction trans=null;
TransactionPool transactionPool = null;
JSONArray myArrayOfRefs= new JSONArray();
JSONObject myObject = new JSONObject();


try {
transactionPool = TransactionPool.get();
trans = transactionPool.beginWork();

Map<String, Object> optionalParams = new HashMap<String, Object>();
optionalParams.put("customerId", custID);


String selectQuery ="SELECT orderId AS ref FROM table1 WHERE custId='[customerId]';" //with 'AS' you can customize your output to any JSON name
JSONArray myArrayOfRefs = null;
ResultSet rs = mySQLTransactionHandle.query(selectQuery, optionalParams);
if(rs.next()) {
myArrayOfRefs = SQL2JSON.convert(rs);
}

//add JSON array to a JSON object
myObject.put("rel_act", myArrayOfRefs); //Add null check.

} catch (Exception e) {
e.printStackTrace(); //Add logging
}
finally
{
try {
transactionPool.commit(mySQLTransactionHandle);
} catch (SQLException e1) {
e1.printStackTrace(); //Add logging
}
}
return myObject;
}

关于java - 使用数据库中的数据创建 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20884455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com