3

I have a web service that performs a database query, converts the result set to a JSON String and returns the strung to the client. This is the code for the converter (I got it from http://biercoff.com/nice-and-simple-converter-of-java-resultset-into-jsonarray-or-xml/):

public static String 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.add(obj);
    }
    return jsonArray.toJSONString();
}

In the client application when I print the returned string it is in the following format:

[{"Column1":0.333333,"Column2":"FirmA"},{"Column1":0.666667,"Column2":"FirmB"}]

so far all is good. The problem I am having is converting the returned string into a JSON array. I tried this:

JSONArray arr = new JSONArray(JSON_STRING);

but got the following error message: constructor JSONArray in class JSONArray cannot be applied to given types. I tried to first convert in into a JSON object like so:

JSONObject obj = new JSONObject(JSON_STRING);

but got the following error: incompatible types: String cannot be converted to Map. What am I doing wrong? Thanks.

user3245747
  • 805
  • 2
  • 17
  • 30
  • See syntax of a json array http://www.w3schools.com/json/json_syntax.asp, the returned string should be in this format arr: [{}, {}] – 6ton Jan 08 '15 at 15:13
  • This is the format of the returned string that I have shown above. I don't see any discrepancy. – user3245747 Jan 08 '15 at 15:15
  • You are missing "arr":, instead its just the array value in your string. – 6ton Jan 08 '15 at 15:18
  • Please change "total_rows" to "total_columns". – Hot Licks Jan 08 '15 at 17:07
  • 6ton I have produced the string in the form: {"Chart":[{"Column1":0.333333,"Column2":"FirmA"} ,{"Column1":0.666667,"Column2":"FirmB"}]} but Im still having the exact problems. – user3245747 Jan 08 '15 at 21:03
  • Kindly share which json library you are using with version number. This seems to be the issue of the version of library that you are using. – Vicky Jan 09 '15 at 08:22
  • I was using the json-simple-1.1.1.jar file. I changed it to java-json.ajr which I got from http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm and the code worked. – user3245747 Jan 09 '15 at 09:29

3 Answers3

1

Apparently the problem was with the json library that I was using. Once I used the

import org.json.JSONArray;

it all worked out well. I was able to convert the returned string to an array using

JSONArray arr = new JSONArray(JSON_STRING);

and to iterate through the values I used the code provided in this answer: Accessing members of items in a JSONArray with Java which I reproduce here for simplicity:

for (int i = 0; i < arr.length(); ++i) {
    JSONObject rec = arr.getJSONObject(i);
    int id = rec.getInt("id");
    String loc = rec.getString("loc");
    // ...
}
Community
  • 1
  • 1
user3245747
  • 805
  • 2
  • 17
  • 30
  • I think that will not be correct with the String because string can be anything u need to convert an array to string. – Kishan Bheemajiyani Jan 09 '15 at 07:49
  • The method posted in the question returns an array which is converted to a string. When I changed the json library I was using I was able to create an array from the string on the client side. The jar which worked for me came from http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm – user3245747 Jan 09 '15 at 09:31
0

well you need to do it by this way for example

String jsonText = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
        try {

            JSONArray array = new JSONArray(jsonText);
            System.out.println(array);
            System.out.println(array.length());

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
0

Check the correct library used. The mess is that org.json.simple is often suggested by IDE as default for JSONObject and JSONArray.

You can find a link to latest jar for org.json library above.

Zon
  • 18,610
  • 7
  • 91
  • 99