创建JSONArray时出现Java JSONException

时间:2011-12-06 21:37:08

标签: java json

我在从字符串创建JSONException时面临JSONArray。我已经使用JSON Validator验证了来自Webservice的响应字符串。

这是代码:

json = new JSONObject(); 
content = recieveData(json.toString(), m_sTimeTableUrl + sPGId);
if (content != null){
  Log.d(TAG, "got content:"+content);
  try {
    JSONArray jArray2 = new JSONArray(content);     //THE EXCEPTION FIRES HERE!
    Log.i(TAG, "Number of entries " + jArray2.length());
    Happening pHappening = null;
    ArrayList<Happening> pResult = new ArrayList<Happening>();
    for(int i = 0; i < jArray2.length(); i++){
      pHappening = new Happening();
      JSONObject jObject = jArray2.getJSONObject(i);
      pHappening.setEndtime(new Time(jObject.getInt("EndMinute")));
      pHappening.setDocent(jObject.getString("Lecturer"));
      pHappening.setRoom(jObject.getString("Room"));
      pHappening.setStartTime(new Time(jObject.getInt("StartMinute")));
      pHappening.setName(jObject.getString("Title"));
      pHappening.setDayOfWeek(jObject.getInt("Weekday"));
      pResult.add(pHappening);
    }
            //java.util.Arrays.sort(pResult);
   return pResult;
  }catch (JSONException e) {
    Log.e(TAG, e.toString());
  }  

这就是我的内容字符串的样子:

{ "Happenings" : [ { "EndMinute" : 570,
        "Lecturer" : "Prof. Dr. Christian Schrödter",
        "OrgLecturId" : 10181,
        "Room" : "KC112",
        "StartMinute" : 480,
        "Title" : "Physik 1 (310321) ",
        "Weekday" : 0
      },
      { "EndMinute" : 675,
        "Lecturer" : "Prof. Dr.-Ing. Norbert Wellerdick",
        "OrgLecturId" : 1750,
        "Room" : "KD010",
        "StartMinute" : 585,
        "Title" : "TM 1 (310341) ",
        "Weekday" : 0
      },
      { "EndMinute" : 780,
        "Lecturer" : "Prof. Dr. Christian Schrödter",
        "OrgLecturId" : 10181,
        "Room" : "KC112",
        "StartMinute" : 690,
        "Title" : "Physik 1 (310321) ",
        "Weekday" : 0
      },
      { "EndMinute" : 930,
        "Lecturer" : "Prof. Dr. Christian Schrödter; Dipl.-Ing. (FH) Bernd Bleyel",
        "OrgLecturId" : 1742,
        "Room" : "KA303",
        "StartMinute" : 840,
        "Title" : "Info 1 (310351) ",
        "Weekday" : 0
      },
      { "EndMinute" : 570,
        "Lecturer" : "Prof. Dr.-Ing. Hermann Lanfer",
        "OrgLecturId" : 1753,
        "Room" : "KC133",
        "StartMinute" : 480,
        "Title" : "ET 1 (310331) ",
        "Weekday" : 1
      },
      { "EndMinute" : 675,
        "Lecturer" : "Prof. Dr. Christian Schrödter; Dipl.-Ing. (FH) Bernd Bleyel",
        "OrgLecturId" : 1742,
        "Room" : "KA303",
        "StartMinute" : 585,
        "Title" : "Info 1 (310351) ",
        "Weekday" : 1
      },
      { "EndMinute" : 780,
        "Lecturer" : "Prof. Dr.-Ing. Axel Schenk",
        "OrgLecturId" : 1812,
        "Room" : "KD010",
        "StartMinute" : 690,
        "Title" : "Mathe 1 (310311) ",
        "Weekday" : 1
      },
      { "EndMinute" : 570,
        "Lecturer" : "Prof. Dr. Christian Schrödter",
        "OrgLecturId" : 8357,
        "Room" : "KC112",
        "StartMinute" : 480,
        "Title" : "Info 1 (310351) ",
        "Weekday" : 2
      },
      { "EndMinute" : 675,
        "Lecturer" : "Prof. Dr.-Ing. Hermann Lanfer",
        "OrgLecturId" : 1753,
        "Room" : "KC112",
        "StartMinute" : 585,
        "Title" : "ET 1 (310331) ",
        "Weekday" : 2
      },
      { "EndMinute" : 780,
        "Lecturer" : "Prof. Dr.-Ing. Robert Paspa",
        "OrgLecturId" : 1764,
        "Room" : "KC112",
        "StartMinute" : 690,
        "Title" : "Konstruk.1 (310381) ",
        "Weekday" : 2
      },
      { "EndMinute" : 930,
        "Lecturer" : "Prof. Dr.-Ing. Axel Schenk",
        "OrgLecturId" : 1812,
        "Room" : "KC112",
        "StartMinute" : 840,
        "Title" : "Mathe 1 (310311) ",
        "Weekday" : 2
      },
      { "EndMinute" : 570,
        "Lecturer" : "Birgitta Götzelmann-Liebig",
        "OrgLecturId" : 8324,
        "Room" : "KC113",
        "StartMinute" : 480,
        "Title" : "TechEngl1 (310391) ",
        "Weekday" : 3
      },
      { "EndMinute" : 675,
        "Lecturer" : "Birgitta Götzelmann-Liebig",
        "OrgLecturId" : 8324,
        "Room" : "KC113",
        "StartMinute" : 585,
        "Title" : "TechEngl1 (310391) ",
        "Weekday" : 3
      },
      { "EndMinute" : 780,
        "Lecturer" : "Prof. Dr.-Ing. Hermann Lanfer",
        "OrgLecturId" : 1753,
        "Room" : "KD010",
        "StartMinute" : 690,
        "Title" : "ET 1 (310331) ",
        "Weekday" : 3
      },
      { "EndMinute" : 930,
        "Lecturer" : "Prof. Dr.-Ing. Axel Schenk",
        "OrgLecturId" : 1812,
        "Room" : "KD010",
        "StartMinute" : 840,
        "Title" : "Mathe 1 (310311) ",
        "Weekday" : 3
      }
    ] }

期待一些帮助!

谢谢!

1 个答案:

答案 0 :(得分:1)

JSONArray jArray2 = new JSONArray(content);

正在尝试使用JSONObject初始化JSONArray。你需要使用

JSONObject obj = new JSONObject(content);

因为content是JSON对象。

但是,键“Happenings”的值是 JSONArray,因此您可以使用它来初始化JSONArray。