user3049183 user3049183 - 4 months ago 25
Java Question

Can not find the Json object (Using org.json)

Microsoft Academic provided an API to get some general information from Microsoft academic. The response type is a Json Object. Using org.Json and following code, I have tried to read the response object but I have failed (need to download these jars + common-logging and common-codec) :

URIBuilder builder = new URIBuilder("");

builder.setParameter("expr", "Composite(AA.AuN=='jaime teevan')");
builder.setParameter("count", "100");
builder.setParameter("attributes", "Ti,CC");

URI uri =;
HttpGet request = new HttpGet(uri);

request.setHeader("Ocp-Apim-Subscription-Key", "df6e26f6e3d145edbbe6aefb5b84af9a");

HttpClient httpclient = HttpClients.createDefault();

HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();

if (entity != null) {

JSONObject obj = new JSONObject(entity);

JSONArray arr = obj.getJSONArray("entities");
for (int i = 0; i < arr.length(); i++){

String post_id = arr.getJSONObject(i).getString("Ti");



Which returns the following exception:

Exception in thread "main" org.json.JSONException: JSONObject["entities"] not found.
at org.json.JSONObject.get(
at org.json.JSONObject.getJSONArray(

How to fix this?

Although it is easy to see an example of the response from the link I provided at the beginning of my question (Microsoft Academic), but for ease of readers I show it in here:

"expr": "Composite(AA.AuN=='jaime teevan')",
"logprob": -15.08,
"Ti": "personalizing search via automated analysis of interests and activities",

"CC": 372,
"logprob": -15.389,
"Ti": "the perfect search engine is not enough a study of orienteering behavior in directed search",
"CC": 237,



Seems like the problem to me is you are not converting your response to string , you need to convert your response to string before passing it to JSONObject

    HttpEntity entity = response.getEntity();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
    } catch (IOException e1) {
    String contentString = new String(os.toByteArray());

or other way is

InputStream instream = entity.getContent();
 BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
    } catch (IOException e) {
    } finally {
        try {
        } catch (IOException e) {
String contentString  = sb.toString(); //  you can pass sb.toString() directly to jsonobject as well

and now pass contentString to JSONObject

 JSONObject obj = new JSONObject(contentString);
 JSONArray arr = obj.getJSONArray("entities");

Update : your can also use this which is also suggested by @Ömer Fadıl Usta but i would strongly recommend to use HttpURLConnection for security and performance