首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到Json对象(使用org.json)

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

Stack Overflow用户
提问于 2016-09-20 05:36:05
回答 2查看 2.7K关注 0票数 4

微软学术提供了一个API来获取微软学术方面的一些一般信息。响应类型是Json对象。使用org.Json和下面的代码,我尝试读取响应对象,但失败了(需要下载这些 jars +公共日志记录和公共编解码器):

代码语言:javascript
复制
    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));
        }

返回以下异常:

代码语言:javascript
复制
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)

怎么解决这个问题?

编辑虽然很容易从我在问题开头提供的链接(微软学术版)中看到响应的例子,但为了方便读者,我在这里展示了它:

代码语言:javascript
复制
    {
  "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,


    }
  ]
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-20 06:17:49

在我看来,问题在于您没有将响应转换为string,您需要在将响应传递给JSONObject之前将响应转换为string

代码语言:javascript
复制
    HttpEntity entity = response.getEntity();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        entity.writeTo(os);
    } catch (IOException e1) {
    }
    String contentString = new String(os.toByteArray());

或者其他方式是

代码语言:javascript
复制
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

代码语言:javascript
复制
 JSONObject obj = new JSONObject(contentString);
 JSONArray arr = obj.getJSONArray("entities");

更新:您也可以使用,这也是@merıl Usta建议的,但我强烈建议在安全性和性能方面使用HttpURLConnection

票数 1
EN

Stack Overflow用户

发布于 2016-09-20 06:34:39

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

代码语言:javascript
复制
if (entity != null) {
    String jsonData = EntityUtils.toString(entity); 
    JSONObject obj = new JSONObject(jsonData);
    ........
    .....
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39586409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档