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.

7
  • See syntax of a json array w3schools.com/json/json_syntax.asp, the returned string should be in this format arr: [{}, {}] Commented Jan 8, 2015 at 15:13
  • This is the format of the returned string that I have shown above. I don't see any discrepancy. Commented Jan 8, 2015 at 15:15
  • You are missing "arr":, instead its just the array value in your string. Commented Jan 8, 2015 at 15:18
  • Please change "total_rows" to "total_columns". Commented Jan 8, 2015 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. Commented Jan 8, 2015 at 21:03

3 Answers 3

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");
    // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think that will not be correct with the String because string can be anything u need to convert an array to string.
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 java2s.com/Code/Jar/j/Downloadjavajsonjar.htm
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();
        }
    }

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.