Right now i'm working with SOAP UI and responce i get in ResultSet format. Since i preffer to work more with JSON rather then ResultSet, i found this nice solution that perfectly fits my needs.
Here's the code :
Thanks to this blog
Here's the code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import java.sql.ResultSet; | |
/** | |
* Utility for converting ResultSets into some Output formats | |
* @author marlonlom | |
*/ | |
public class Convertor { | |
/** | |
* Convert a result set into a JSON Array | |
* @param resultSet | |
* @return a JSONArray | |
* @throws Exception | |
*/ | |
public static JSONArray convertToJSON(ResultSet resultSet) | |
throws Exception { | |
JSONArray jsonArray = new JSONArray(); | |
while (resultSet.next()) { | |
int total_rows = resultSet.getMetaData().getColumnCount(); | |
JSONObject obj = new JSONObject(); | |
for (int i = 0; i < total_rows; i++) { | |
obj.put(resultSet.getMetaData().getColumnLabel(i + 1) | |
.toLowerCase(), resultSet.getObject(i + 1)); | |
jsonArray.put(obj); | |
} | |
} | |
return jsonArray; | |
} | |
/** | |
* Convert a result set into a XML List | |
* @param resultSet | |
* @return a XML String with list elements | |
* @throws Exception if something happens | |
*/ | |
public static String convertToXML(ResultSet resultSet) | |
throws Exception { | |
StringBuffer xmlArray = new StringBuffer("<results>"); | |
while (resultSet.next()) { | |
int total_rows = resultSet.getMetaData().getColumnCount(); | |
xmlArray.append("<result "); | |
for (int i = 0; i < total_rows; i++) { | |
xmlArray.append(" " + resultSet.getMetaData().getColumnLabel(i + 1) | |
.toLowerCase() + "='" + resultSet.getObject(i + 1) + "'"); } | |
xmlArray.append(" />"); | |
} | |
xmlArray.append("</results>"); | |
return xmlArray.toString(); | |
} | |
} |
Hi,
ReplyDeletewhen I copy the code to eclipse, I get an error for line 44. There seems to be a problem with the quotes.
You're right - the problem was not in code, but in Blogger source code parser - it got broken. I made a Gist snipped, which you can find here - https://gist.github.com/azakordonets/da31d9e74369a9d71b1c . Also, on my personal blog post - http://biercoff.com/nice-and-simple-converter-of-java-resultset-into-jsonarray-or-xml/ formatting is not broken, so for new articles you can look at http://biercoff.com.
ReplyDeleteThanks for noticing
Hi , shouldn't line 26 be out of that inner loop ?
ReplyDeleteYou can see latest updated version here - http://biercoff.com/nice-and-simple-converter-of-java-resultset-into-jsonarray-or-xml/
DeleteThe code is full of errors.
ReplyDeleteBelow is the rectified code for convertToJSON()
public static JSONArray convertToJSON(ResultSet resultSet) throws Exception {
JSONArray jsonArray = new JSONArray();
JSONObject obj;
int total_cols = resultSet.getMetaData().getColumnCount();
while (resultSet.next()) {
obj = new JSONObject();
for (int i = 0; i < total_cols; i++) {
obj.put(resultSet.getMetaData().getColumnLabel(i + 1),
resultSet.getObject(i + 1));
}
jsonArray.put(obj);
}
return jsonArray;
}
Looks like you don't read comments :) As i said, i don't keep this article updated, rather i moved everything to my personal blog.
Delete