Arraying Arraying - 1 year ago 84
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

Answer Source

@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());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download