Arraying Arraying - 3 months ago 34
Java Question

JSON Object Returns Null

I have a very strange error when trying to get a number from a JSON API; the object seems to be null, although the URL (and code) should be correct.

org.json.JSONException: JSONObject["success"] not found.

I have tried printing out the JSONObject, and it gives me this:


Here is my code:

String url = "";
JSONObject jsonObject = new JSONObject(new URL(url).openStream());
String resultType = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("data");
int number = jsonArray.getInt(0);
//do stuff with number
catch(Exception e){
//handle catch


@Andreas is right, add this piece of code in your try block to convert input stream into a json string -

InputStream is = new URL(url).openStream();
int ch;
StringBuilder sb = new StringBuilder();
while((ch = != -1)
JSONObject jsonObject = new JSONObject(sb.toString());