我尝试做的是根据我的应用中发生的事情,从不同的链接(4个不同的链接)加载我的JSON。它是一个拥有4个不同电台的无线电网络应用程序。我可以将当前正在播放的电台推送到我的JSON ViewController,但问题是我如何让JSON做一个"如果这个当前电台正在播放那么来自这个链接:链接到JSON信息"?我知道它可能但不知道如何实现它。这是我使用交换机更新的JSON ViewController的代码,但没有运气:
class TableViewController: UITableViewController {
//1. Create An Array To Store The SongData
var songs = [SongData]()
var currentStation: RadioStation!
var downloadTask: URLSessionDownloadTask?
override func viewDidLoad() { super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
//2. Load The JSON From The Main Bundle
struct RadioStation {
var name: String
var streamURL: String
var imageURL: String
var desc: String
var longDesc: String
let currentStation = RadioStation!(name: String)
var reqStr = ""
switch currentStation {
case "SPIN":
reqStr = "http://streamdb3web.securenetsystems.net/player_status_update/JACKSON1_history.txt"
case "The FUZE":
reqStr = "http://streamdb3web.securenetsystems.net/player_status_update/JACKSON2_history.txt"
case "The RANCH":
reqStr = "http://streamdb3web.securenetsystems.net/player_status_update/JACKSON3_history.txt"
case "SAGE":
reqStr = "http://streamdb3web.securenetsystems.net/player_status_update/JACKSON4_history.txt"
default:
reqStr = "http://streamdb3web.securenetsystems.net/player_status_update/JACKSON1_history.txt"
let urlText = URL (string: reqStr)
do{
//a. Get The Data From The From The File
let data = try Data(contentsOf: urlText!)
//b. Decode The Data To Our Structs
let albumData = try JSONDecoder().decode(Response.self, from: data)
//c. Append The Songs Array With The PlayHistory
albumData.playHistory.song.forEach { songs.append($0) }
//d. Test Some Data
print("""
**The First Album Details**
Album = \(songs[0].album)
Artist = \(songs[0].artist)
Cover = \(songs[0].cover)
Duration = \(songs[0].duration)
Start = \(songs[0].programStartTS)
Title = \(songs[0].title)
""")
//3. Load The Data
DispatchQueue.main.async {
self.tableView.reloadData()
}
}catch{
print(error)
}
}
}
}`
以下代码是我用上面的开关更新之前的原始代码。
'class TableViewController: UITableViewController {
//1. Create An Array To Store The SongData
var songs = [SongData]()
var currentStation: RadioStation!
var downloadTask: URLSessionDownloadTask?
override func viewDidLoad() { super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
//2. Load The JSON From The Main Bundle
guard let urlText = URL (string: "http://streamdb3web.securenetsystems.net/player_status_update/JACKSON1_history.txt")
else { return }
do{
//a. Get The Data From The From The File
let data = try Data(contentsOf: urlText)
//b. Decode The Data To Our Structs
let albumData = try JSONDecoder().decode(Response.self, from: data)
//c. Append The Songs Array With The PlayHistory
albumData.playHistory.song.forEach { songs.append($0) }
//d. Test Some Data
print("""
**The First Album Details**
Album = \(songs[0].album)
Artist = \(songs[0].artist)
Cover = \(songs[0].cover)
Duration = \(songs[0].duration)
Start = \(songs[0].programStartTS)
Title = \(songs[0].title)
""")
//3. Load The Data
DispatchQueue.main.async {
self.tableView.reloadData()
}
}catch{
print(error)
}
}