[英]Can not find the Json object (Using org.json)
Microsoft Academic provided an API to get some general information from Microsoft academic. Microsoft Academic提供了一个API,用于从Microsoft学术中获取一些常规信息。 The response type is a Json Object.
响应类型是Json对象。 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) :
使用org.Json和以下代码,我试图读取响应对象,但我失败了(需要下载这些 jar + common-logging和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", "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));
}
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: 编辑虽然很容易看到我在问题开头提供的链接响应示例(Microsoft Academic),但为了方便读者,我在这里展示:
{
"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,
}
]
}
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
对我来说似乎问题是你没有将你的响应转换为
string
,你需要在将响应转换为JSONObject
之前将其转换为string
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
现在将
contentString
传递给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 更新:您也可以使用@ÖmerFadılUsta建议的这个 ,但我强烈建议使用
HttpURLConnection
来提高安全性和性能
Try to pass string JsonData to JSONObject : 尝试将字符串JsonData传递给JSONObject:
if (entity != null) {
String jsonData = EntityUtils.toString(entity);
JSONObject obj = new JSONObject(jsonData);
........
.....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.