找不到 Json 对象(使用 org.json)

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 和 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));
        }

其中returns以下异常:

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)

如何解决这个问题?

编辑 虽然很容易看到我在问题开头提供的 link 的响应示例 (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,


    }
  ]
}

对我来说问题似乎是你没有将你的回复转换为 string ,你需要将你的回复转换为 string 然后再传递给 JSONObject

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

或者其他方式是

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

现在将 contentString 传递给 JSONObject

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

更新:您也可以使用 this,@Ömer Fadıl Usta 也建议这样做 但为了安全和性能,我强烈建议使用 HttpURLConnection

尝试将字符串 JsonData 传递给 JSONObject :

if (entity != null) {
    String jsonData = EntityUtils.toString(entity); 
    JSONObject obj = new JSONObject(jsonData);
    ........
    .....
}