我从XML输出克隆了以下代码。我需要输出是一个JSON对象而不是XML字符串,但我遗漏了一些东西。
public String evaluate() throws Exception
{
String itemTitle = "";
BoundStringBuilder data = new BoundStringBuilder();
TreeMap promptValues = ReportItem.promptValuesSetStringToTreeMap(promptValuesSet);
String debugFlag = isDebugMode ? "Y" : "N";
// get JSON item config settings
ExecuteProcedure ep = new ExecuteProcedure(con, "pkg_report.select_mr_JSON_config");
ep.setParam("p_report_number", reportNumber);
ep.setParam("p_instance_number", instanceNumber);
ep.setParam("p_item_index", itemIndex);
ep.setParam("p_path", null);
ResultSet JSONConfigRs = ep.execute()[0];
while(JSONConfigRs.next())
{
String setting = JSONConfigRs.getString("field_name");
String value = UtilityMethods.nullValue(JSONConfigRs.getString("value"), "").toLowerCase();
if(setting.equals("JSON_JSONREPORTITEM_ITEMNAME_BAN_ITEM_NAME"))
itemTitle = value;
}
JSONConfigRs.close();
ep.close();
// execute sql
ep = new ExecuteProcedure(con, "pkg_report.execute_mr_sql");
ep.setParam("p_report_number", reportNumber);
ep.setParam("p_instance_number", instanceNumber);
ep.setParam("p_item_index", itemIndex);
ep.setParam("p_item_type_code", itemTypeCode);
ep.setParam("p_sub_item_index", "1");
ep.setParam("p_sub_sub_item_index", "1");
ep.setParam("p_prompt_values", UtilityMethods.treeMapToList(promptValues));
ep.setParam("p_debug_flag", debugFlag);
ResultSet rs = ep.execute()[0];
ResultSetMetaData rsmd = rs.getMetaData();
String[] columnNames = new String[rsmd.getColumnCount()];
for(int i = 0; i < columnNames.length; i++)
columnNames[i] = rsmd.getColumnName(i + 1).toLowerCase();
data.append("<data name=\"").append(itemTitle).append("\">\n");
while(rs.next())
{
data.append("\t<row>\n");
for(int i = 0; i < columnNames.length; i++)
{
data.append("\t\t<cell name=\"").append(columnNames[i]).append("\">");
data.append(UtilityMethods.xmlFormat(UtilityMethods.screenFormat(rs.getString(i + 1)).trim()));
data.append("</cell>\n");
}
data.append("\t</row>\n");
}
rs.close();
ep.close();
data.append("</data>\n");
String JSONData = data.toString();
return JSONData;
}
我被告知“您应该使用GSON JSON实用程序方法将结果集转储到JSON对象,而不是构建XML字符串。”
请有人指出我正确的方向。我不熟悉JSON对象,如果这不够明显;)
答案 0 :(得分:2)
json.org有一个可以使用的简单API:http://json.org/java/
你可以这样做:
column = {“column1”:123“}
row = [{“column1”:123“},{”column2“:456”}]
rows = [[{“column1”:123“},{”column2“:456”}],[{“column1”:111“},{”column2“:222”}]]
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
JSONArray rows = new JSONArray();
while(rs.next())
{
JSONArray columns = new JSONArray();
for(int i = 0; i < columnNames.length; i++)
{
JSONObject jsonObj = new JSONObject();
jsonObj.put(columnNames[i], rs.getString(i + 1));
columns.put(jsonObj);
}
rows.put(columns);
}
rs.close();
return rows.toString();
答案 1 :(得分:0)
以下是对我有用的修复:
我删除了所有xml和表构建(行/列/数据)信息,只是将结果集转储到GSON JSON数组中并输出json。代码如下:
JsonArray reportArray = JsonUtilities.resultSetToJsonObjectArray(ep.execute()[0]); //Dump Result Set to JsonArray
ResultSetMetaData rsmd = rs.getMetaData();
String[] columnNames = new String[rsmd.getColumnCount()];
for(int i = 0; i < columnNames.length; i++)
columnNames[i] = rsmd.getColumnName(i + 1).toLowerCase();
rs.close();
ep.close();
Gson gson = new GsonBuilder().setPrettyPrinting().create(); //Create JSON object and format output for readability
String JSONData = gson.toJson(reportArray);
return JSONData;