gpt4 book ai didi

arrays - 如何将多个嵌套的 JSON 响应解析为数组?

转载 作者:行者123 更新时间:2023-11-28 06:38:50 27 4
gpt4 key购买 nike

我想转换我的 JSON 响应,但它抛出了一个错误。我不知道如何正确解析 JSON 以使字段可访问。我怎样才能做到这一点?

 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]]

print(self.schoolDetailsCollection)

print(self.schoolDetailsList)


}

}

此行抛出以下错误:

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 = (
"Milner_college_3.jpeg",
"Milner_college_2.jpeg.png",
"Milner_college_4.jpeg"
);
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" = "https://www.milner.wa.edu.au/";
};
}

最佳答案

检查 if let block 以转换响应对象并解析它。

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
}
}

关于arrays - 如何将多个嵌套的 JSON 响应解析为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38502781/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com