JSON异常:JSONObject xxxx不是JSONObject

时间:2013-04-19 15:46:09

标签: java json exception tags

我想使用这个类

https://code.google.com/p/ext-c/source/browse/trunk/src/nz/ac/vuw/ecs/kcassell/similarity/GoogleDistanceCalculator.java

这是我的代码(我的目标是计算所有代码之间的成对Google距离)

public static void main(String args[]) throws FileNotFoundException {
   Scanner s = new java.util.Scanner(new File (yyy));
   try {
      GoogleDistanceCalculator googleDistanceCalculator =
         new GoogleDistanceCalculator();
      ArrayList<String>  Alltags = new ArrayList<String>();
      while (s.hasNext()) {
         Alltags.add(s.next());
      }
      ArrayList<Double>  arraysNGD = new ArrayList<Double>();
      for (int i = 0; i < Alltags.size(); i++) {
         for (int j = 0; j < Alltags.size(); j++) {
            if (j!=i)
               arraysNGD.add(
                  googleDistanceCalculator.calculateDistance(
                     Alltags.get(i).trim(), Alltags.get(j).trim()));
         }
      }
   } catch (NumberFormatException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }
}

错误

org.json.JSONException: JSONObject["responseData"] is not a JSONObject.
  at org.json.JSONObject.getJSONObject(JSONObject.java:508)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.getCountFromGoogleQuery(GoogleDistanceCalculator.java:196)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.getCountFromQuery(GoogleDistanceCalculator.java:173)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.numResultsFromWeb(GoogleDistanceCalculator.java:150)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.calculateDistance(GoogleDistanceCalculator.java:262)
  at test.Zssai.main(Zssai.java:55)

NB

当我尝试例如

googleDistanceCalculator.calculateDistance("forest","plant");

没有错误,工作正常

修改

nature  sky
  org.json.JSONException: JSONObject["responseData"] is not a JSONObject.
  at org.json.JSONObject.getJSONObject(JSONObject.java:508)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.getCountFromGoogleQuery(GoogleDistanceCalculator.java:196)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.getCountFromQuery(GoogleDistanceCalculator.java:173)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.numResultsFromWeb(GoogleDistanceCalculator.java:150)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.calculateDistance(GoogleDistanceCalculator.java:262)
  at test.Zssai.main(Zssai.java:51)

nature  blue
  org.json.JSONException: JSONObject["responseData"] is not a JSONObject.
  at org.json.JSONObject.getJSONObject(JSONObject.java:508)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.getCountFromGoogleQuery(GoogleDistanceCalculator.java:196)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.getCountFromQuery(GoogleDistanceCalculator.java:173)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.numResultsFromWeb(GoogleDistanceCalculator.java:150)
  at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.calculateDistance(GoogleDistanceCalculator.java:262)
at test.Zssai.main(Zssai.java:51)
    nature  water
    org.json.JSONException: JSONObject["responseData"] is not a JSONObject.
at org.json.JSONObject.getJSONObject(JSONObject.java:508)
at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.getCountFromGoogleQuery(GoogleDistanceCalculator.java:196)
at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.getCountFromQuery(GoogleDistanceCalculator.java:173)
at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.numResultsFromWeb(GoogleDistanceCalculator.java:150)
at nz.ac.vuw.ecs.kcassell.similarity.GoogleDistanceCalculator.calculateDistance(GoogleDistanceCalculator.java:262)
at test.Zssai.main(Zssai.java:51)
    nature  clouds

1 个答案:

答案 0 :(得分:0)

问题在于

雅虎老板不是免费的

并且Google搜索API已被删除 所以它只允许限制数量的查询 当我超过这个数字时,我得到了错误

    org.json.JSONException: JSONObject["responseData"] is not a JSONObject.

所以要解决这个问题,我应该使用其他搜索引擎API

此API可能很有用

   http://www.sindice.com/