2

I'm trying to convert the JSON data that i get back to an array so I can use the data in a listView.

I got the following code:

JSONObject jsonobject;
jsonobject = JSONFunctions.getJSONfromURL("example/url");

ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonobject;
if (jsonArray != null) {
   //do something with it
}

Note: I don't have any experience with Java.
The method getJSONfromURL returns the JSON of the given URL that works just fine but the error is in JSONArray jsonArray = (JSONArray)jsonobject;

It gives the following error: cannot cast JSONObject to JSONArray I've also tried this: JSONArray jsonArray = (JSONObject)(JSONArray)jsonobject;

I can't figure out what i'm doing wrong.

So how can I cast my jsonobject to a normal array that I can use as data for my listView?

9
  • 1
    Which library are you using? Commented Feb 25, 2015 at 13:57
  • I'd take a look at the JSON you're getting back from the URL (and post it here if you can). Are you sure it's an array? Generally speaking, returning a top-level array is considered a security vulnerability. Commented Feb 25, 2015 at 14:08
  • This? json.org/javadoc/index.html There is no class named JSONFunctions. Commented Feb 25, 2015 at 14:08
  • Yes that's what i'm using. Im sorry for the misunderstanding JSONFunctions is a class that I wrote myself. Commented Feb 25, 2015 at 14:09
  • If you expect an array, you can use new JSONArray(String). Commented Feb 25, 2015 at 14:12

2 Answers 2

1

A JSONObject is distinct from a JSONArray. If you try new JSONObject("['test', 'array']"), you will get a JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1].

Try whether this works for you:

static JSONArray fromUrl(URL url) throws IOException {
    try (
        InputStream openStream = url.openStream();
        BufferedInputStream bis = new BufferedInputStream(openStream);
        Scanner scanner = new Scanner(bis, StandardCharsets.UTF_8.name());
    ) {
        if (!scanner.useDelimiter("\\A").hasNext()) {
            throw new EOFException("empty response");
        }
        return new JSONArray(scanner.next());
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer but your code throws an error at the last line return new JSONArray. Error: Unhandled exception:JSONException
@Bas In the sources I downloaded (github.com/douglascrockford/JSON-java/blob/master/…) it doesn't need to be handled because it extends RuntimeException. Then you have to handle the exception by adding a catch-clause or declare it to be thrown by putting it in the throws-clause (throws IOException, JSONException). But this is basic Java knowledge you should definitely acquire.
0

JSONObject is like a Map in java. Key-value pair. So, you should iterate like iterating a Map:

Iterator iterator = jsonobject.keys();
JSONArray jsonArray = new JSONArray();

while (iterator.hasNext()){
    String key = (String) iterator.next(); // assuming the key to be a String
    // You can put the key also if you want. But, It does not make any sense to do so.
    jsonArray.put(jsonobject.get(key)); 
}

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.