user3049183 user3049183 - 2 months ago 19
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("https://api.projectoxford.ai/academic/v1.0/evaluate?");

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

URI uri = builder.build();
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");
System.out.println(post_id);

}

System.out.println(EntityUtils.toString(entity));
}


Which returns the following exception:

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


How to fix this?

EDIT
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')",
"entities":
[
{
"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,


}
]
}

Answer

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 {
        entity.writeTo(os);
    } 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) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
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