Microsoft Academic提供了一個API來獲取來自Microsoft學術機構的一些常規信息。響應類型是Json對象。使用org.Json和下面的代碼,我試圖讀取響應對象,但我失敗了(需要下載these罐+共記錄和常見的編解碼器):找不到Json對象(使用org.json)
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", "Your-Key");
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));
}
它返回以下異常:
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)
如何解決這個問題?
編輯 雖然很容易看到我在我的問題(微軟學術)年初提供的鏈接響應的例子,但爲了方便讀者的我展現它在這裏:
{
"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,
}
]
}
答案如何?你有可能收到錯誤信息嗎?從錯誤消息中可以看到返回的對象中沒有'entities'屬性。 – Henry
請點擊我的問題中的第一個單詞(Microsoft Academic)以查看回復的示例。當然,當我使用'System.out.println(EntityUtils.toString(實體));'我可以看到整個響應。 – user3049183