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 : 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