Tuesday, November 19, 2013

Nice and simple converter of Java ResultSet into JSONArray or XML

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 :
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();
}
}
Thanks to this blog

6 comments:

  1. Hi,
    when I copy the code to eclipse, I get an error for line 44. There seems to be a problem with the quotes.

    ReplyDelete
  2. 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.

    Thanks for noticing

    ReplyDelete
  3. Hi , shouldn't line 26 be out of that inner loop ?

    ReplyDelete
    Replies
    1. You can see latest updated version here - http://biercoff.com/nice-and-simple-converter-of-java-resultset-into-jsonarray-or-xml/

      Delete
  4. The code is full of errors.

    Below 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;
    }

    ReplyDelete
    Replies
    1. 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