1

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!

5
  • what's the exception?! full stack trace please! Commented Dec 6, 2011 at 21:48
  • Your JSON is an object and not an array. It's probably a cast exception Commented Dec 6, 2011 at 21:50
  • here is full stack tracke from logcat(its an android app). Commented Dec 6, 2011 at 22:02
  • 12-06 21:58:36.561: E/de.hhn.se.mvs.hhnapp.splan.main.SPLANSyncAdapter(15914): org.json.JSONException: Value {"Happenings":.....}]} of type org.json.JSONObject cannot be converted to JSONArray Commented Dec 6, 2011 at 22:06
  • but i dont get it, im not event trying to convert a JSONObject to a JSONArray. Im trying to create a new array out of a string, dont i? Commented Dec 6, 2011 at 22:08

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.