0

I have output from url as following:

["HANK-TPSSL4-app","acgeneral-app","acgeneral-web","achalf-app","acproduct-app"]

My Java code to get each object from this array:

 BufferedReader in = new BufferedReader(new InputStreamReader((HttpURLConnection) alertHandleUrl.openConnection().getInputStream()));

        String output;      
        while ((output = in.readLine()) != null) {

             JSONArray array = new JSONArray(output);

                for (int i =0; i < array.length(); i++)
                    JSONObject row = array.getJSONObject(i);
                    System.out.println(row);
                }

The error I am getting is

Exception in thread "main" org.json.JSONException: JSONArray[0] is not a JSONObject. at org.json.JSONArray.getJSONObject(JSONArray.java:330) at com.ebay.sherlock.calc.AutoConfigFunc.main(AutoConfigFunc.java:70)

Can someone help me? Thanks in advance

2
  • You output should look like that {"myarray":["abc","def"]} then new JSONObject(output).getJSONArray("myarray").... And it should do it. Or try to make your output look like that {["abc","def"]} and new JSONArray(output) Commented Apr 28, 2013 at 3:28
  • Hi, Since my ouput is in the format of ["abc","def"] format, How do I get each object from this JSON array? Thank you.. Commented Apr 28, 2013 at 3:48

1 Answer 1

0

Yes, this output is a simple String array not a JSON object. You need to trim the leading and trailing braces and then split the string by comma ","

2
  • Hi, My ouput is already split by comma ",". Here is my output: ["HANK-TPSSL4-app","acgeneral-app","acgeneral-web","achalf-app","acproduct-app"] Commented Apr 28, 2013 at 3:45
  • @user2325703 yes, but it is string. You need to split it by using split() method and convert it into a String[] array Commented Apr 28, 2013 at 3:47

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.