gpt4 book ai didi

ios - 如何使用 Alamofire 和 SwiftyJSON 正确创建一个类来保存来自 JSON 的数据

转载 作者:行者123 更新时间:2023-11-28 06:41:33 25 4
gpt4 key购买 nike

我正在尝试创建一个类来保存来自以 JSON 格式发送的网络服务的数据。这是我从服务器返回的 JSON:

[  
{
"weekSchedule":[
{
"weekDay":"sunday",
"listening":[
{
"textName":"Programa de teste 1",
"textPresenter":"Apresentador",
"timeStartHour":"08:00:00",
"timeEndHour":"23:59:00",
"textDescription":"Descri\u00e7\u00e3o do programa ",
"textGuest":"Dr. Lair Ribeiro",
"urlImage":"file:\/\/servidor\/lampp\/webservice\/img\/happy.png"
},
{
"textName":"Teste 2",
"textPresenter":"Teste",
"timeStartHour":"00:00:00",
"timeEndHour":"00:00:00",
"textDescription":"",
"textGuest":"",
"urlImage":"file:\/\/servidor\/lampp\/webservice\/img\/happy.png"
}
]
},
{
"weekDay":"monday",
"listening":[
{
"textName":"Programa de teste 1",
"textPresenter":"Apresentador",
"timeStartHour":"08:00:00",
"timeEndHour":"23:59:00",
"textDescription":"Descri\u00e7\u00e3o do programa ",
"textGuest":"Dr. Lair Ribeiro",
"urlImage":"file:\/\/servidor\/lampp\/webservice\/img\/happy.png"
},
{
"textName":"Programa teste marco",
"textPresenter":"Marco",
"timeStartHour":"08:30:00",
"timeEndHour":"09:30:00",
"textDescription":"Apenas um programa para testar o json",
"textGuest":"Jason",
"urlImage":"file:\/\/servidor-ubuntu\/lampp\/webservice\/img\/happy.png"
}
]
}
]
}
]

这是我正在创建的模型类(这里需要帮助!)

import Foundation
import SwiftyJSON

class Show: ResponseJSONObjectSerializable {

var name: String?
var description: String?
var host: String?
var guest: String?
var startTime: String?
var endTime: String?
var urlImage: String?

required init(json: JSON) {
self.name = json["textName"].string
self.description = json["textDescription"].string
self.host = json["textPresenter"].string
self.guest = json["textGuest"].string
self.startTime = json["timeStartHour"].string
self.endTime = json["timeEndHour"].string
self.urlImage = json["urlImage"].string
}

required init() {}
}

class ShowArray: ResponseJSONObjectSerializable {

var weekday: String?
var showArray: [Show]?

required init?(json: JSON) {

self.weekday = json["weekDay"].string

if let jsonArray = json["listening"].array {
self.showArray = []

for json in jsonArray {
let instance = Show(json: json)
self.showArray?.append(instance)
}
}
}

}

class ScheduleArray: ResponseJSONObjectSerializable {

var scheduleArray: [ShowArray]?

required init?(json: JSON) {

if let weekArray = json["weekSchedule"].array {
self.scheduleArray = []

for weekDay in weekArray {
let instance = ShowArray(json: weekDay)
self.scheduleArray?.append(instance!)
}
}
}
}

发出 http 请求的 Alamofire 函数:

func getWeeklyShedule(completionHandler: (Result<ScheduleArray, NSError>) -> Void) {

alamofireManager.request(RCAppRouter.GetSchedule()).responseObject {
(response: Response<ScheduleArray, NSError>) in

guard response.result.isSuccess else {
print("Error fetching schedule: \(response.result.error!)")
return
}

completionHandler(response.result)
}
}

然后在我的 View Controller 中,我需要将列表保存在 Show 数组中

var listings = [Show]()
RCAPIManager.sharedInstance.getWeeklyShedule {
(result) in

guard result.error == nil else {
print("Error")
return
}

guard let fetchedSchedule = result.value else {
print("No schedule fetched")
return
}

self.listings = fetchedSchedule.scheduleArray ???

}

这就是我卡住的地方,我如何循环遍历对象以将列表中的数据存储到我的 Show 数组?

有关如何重构我的代码以实现的任何提示,预期结果是一周中每一天的分段控件,按下时我会将其用作过滤器以显示适当的信息。

谢谢!!!

最佳答案

这可能不是最漂亮的方式,但它会起作用。

if let responseArray = response as? NSArray {
for i in 0..<responseArray.count {
let showDic = responseArray[i] as! NSDictionary
let dayOfWeek = showDic["weekday"] as! String
let showArray = showDic["listening"] as! NSArray
switch dayOfWeek {
case "sunday":
addShowObjectsToArray(showArray, dailyShows: sundayArray)
case "monday":
addShowObjectsToArray(showArray, dailyShows: mondayArray)
case "tuesday":
addShowObjectsToArray(showArray, dailyShows: tuesdayArray)
case "wednseday":
addShowObjectsToArray(showArray, dailyShows: wednsedayArray)
case "thursday":
addShowObjectsToArray(showArray, dailyShows: thursdayArray)
case "friday":
addShowObjectsToArray(showArray, dailyShows: fridayArray)
case "saturday":
addShowObjectsToArray(showArray, dailyShows: saturdayArray)
default:
}
}

func addShowObjectsToArray(shows: NSArray, dailyShows: NSArray) {
for show in shows {
let showObject = Show(show)
dailyShows.append(showObject)
}
}

关于ios - 如何使用 Alamofire 和 SwiftyJSON 正确创建一个类来保存来自 JSON 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37868622/

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