DI MI
DI MI

Reputation: 245

Java JSONException while creating JSONArray

I am facing JSONException while creating a JSONArray from of a string. I have validated the response string from Webservice using JSON Validator.

here is the code:

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

and this is how my content string looks like:

{ "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
      }
    ] }

Looking forward for some help !

Thanks!

Upvotes: 1

Views: 518

Answers (1)

andyb
andyb

Reputation: 43823

JSONArray jArray2 = new JSONArray(content);

is trying to use a JSONObject to initialize a JSONArray. You need to use

JSONObject obj = new JSONObject(content);

since your content is a JSON object.

However, the value of the key "Happenings" is a JSONArray, so you could use that to initialize the JSONArray.

Upvotes: 1

Related Questions