Sorry, possible newbie question, I'm trying to learn Java while contributing to a project at work. Actually code is Groovy (& we're using Grails) but assume that's the same for this purpose.
I'm trying to convert a JDBC ResultSet to JSON (to send to the front end). Got the following code from a blog:
// Convert JDBC ResultSet to JSON string
public static JSONArray convertToJSON(ResultSet resultSet)
throws Exception {
JSONArray jsonArray = new JSONArray();
ResultSetMetaData metaData = resultSet.getMetaData(); // Result set meta data
int total_columns = metaData.getColumnCount(); // Number of columns in the row
while (resultSet.next()) { // Take each row from the result set
JSONObject obj = new JSONObject();
for (int i = 0; i < total_columns; i++) {
obj.put(metaData.getColumnLabel(i + 1)
.toLowerCase(), resultSet.getObject(i + 1));
}
jsonArray.put(obj);
}
return jsonArray.toString(); // Return as JSON string
}
This (I believe) will give me a JSON structure with the data in the root/top of the JSON. I want to move it into a sub-field (called e.g. 'data') and then have another couple of key/value pairs at root level. How would I modify the code to do this please? (I could pass the couple of extra values in as params)
Thanks.