user2039697 user2039697 - 7 months ago 29
Swift Question

How to parse a multiple nested JSON response into array?

I wanted to cast my JSON response but it throws an error. I dont figure it out how to correctly parse the JSON in order to make the fields accessable.
How can I achieve this?

func cellButtonTapped(sender: AnyObject) {

self.restApi.getSchoolDetails(schoolId) {responseObject, error in
// use responseObject and error here

self.schoolDetailsCollection = NSDictionary(dictionary: responseObject! as! [NSObject : AnyObject])

//access the inner array from the json answer called result
self.schoolDetailsList = self.schoolDetailsCollection["result"] as! [[String:AnyObject]]





This line is throwing the following error:

self.schoolDetailsList = self.schoolDetailsCollection["result"] as! [[String:AnyObject]]


Could not cast value of type '__NSCFDictionary' (0x102f3a178) to 'NSArray' (0x102f39b88).

Json: -

result = {
address = " 379 Hay St, Perth WA 6000, Australia";
city = "Perth ";
"cou_id" = AU;
"cur_id" = "";
environment = L;
financed = "<null>";
images = (
intro = "Our language school is one of the most famous in Australia with over 30 years of experience of English teaching. It is situated in the middle of Perth, surrounded by shops, caf\U00e9s and restaurants. The train station can be reached in just a couple of minutes by walking. There is a free shuttle bus with a stop right in front of the school. <br/>The school has 30 classrooms with air-condition, a spacious student lounge, a computer room, a library, a caf\U00e9, a kitchen and a gaming room. Free Wi-Fi covers the whole area. During the breaks, students can meet in the 200m2 garden and can chill out and chat under umbrellas. Highly qualified teachers ensure your progress by providing you a great learning experience.";
leisure = "";
name = "Milner college";
"sco_id" = 3;
"sco_type" = LS;
"sell_point" = "* Provide our students with the best possible standard of teaching.<br/>* Give our students an enjoyable and fulfilling overall experience.<br/>* Give our students value for the money they pay us.<br/>* Work as a team, in which our teachers and other staff trust and support each other.<br/>* Maintain high standards, striving always to improve them.<br/>* Enable our students, by enhancing their language skills to achieve great things.<br/>* Be mindful that we have a duty beyond being an ethical and competent college, by doing some good for humanity.";
"video_url" = "<null>";
"web_url" = "";


check if let block to cast response object and parse it.

if let response = responseObject as? NSDictionary {
  self.schoolDetailsList = response as? NSDictionary
  if let address = response["result"]["address"] as? String {
     print(address) // 379 Hay St, Perth WA 6000, Australia