将TableViewCell从数组转换为JSON

时间:2019-11-27 14:26:56

标签: ios json swift

所以我有一个表,它以快速代码从固定数组返回数据。

但是现在我想将该固定数据切换出此JSON中的数据-https://api.drn1.com.au/api-access/news

但是,因为我在当前代码中使用了关键字news,所以我发现很难调用此JSON,因为我需要像JSONP一样使用关键字news

  

{“新闻”:...}

现在这是当前代码,我知道所有人都需要更改此代码以注入JSON数据。

func createArray() -> [News] {

   return [News(title: "Hello") , News(title: "how") , News(title: "You")]  
}

起初,我认为这就像更改正在播放的json获取数据一样简单。

这是脚本(结构部件未包括在其中):

@objc func nowplaying(){
    let jsonURLString = "https://api.drn1.com.au/station/playing"
    guard let feedurl = URL(string: jsonURLString) else { return }
    URLSession.shared.dataTask(with: feedurl) { (data,response,err) in

    guard let data = data else { return }
    do {
       let nowplaying = try JSONDecoder().decode(Nowplayng.self, from: data)
        nowplaying.data.forEach {
           DispatchQueue.main.async {
             self.artist.text = nowplaying.data.first?.track.artist
             self.song.text = nowplaying.data.first?.track.title
             }
           }
      } catch let jsonErr {
         print("error json ", jsonErr)
    }
  }.resume()
}

我如何使用下面的代码

struct NewsData: Decodable{
    let news: [articalData]
}

struct articalData: Decodeable{
    let title: String
}

获取新闻:

   @objc func newsfetch(){
        let jsonURLString = "https://api.drn1.com.au/api-access/news"
        guard let feedurl = URL(string: jsonURLString) else { return }


    URLSession.shared.dataTask(with: feedurl) { (data,response,err) in

    guard let news = data else { return }
    do {
        let news = try JSONDecoder().decode(NewsData.self, from: news)
           NewsData.news.forEach {
             print(NewsData.news.title)
           }
       } catch let jsonErr{
            print("error json ", jsonErr)
       }
 }.resume()
}

无论何时我遇到错误

第一个1出现在第一个Struct

  

struct NewsData:Decodable {//错误类型'NewsData'不存在   符合协议“可解码”

第二个错误

  

struct articalData:可解码{//使用未声明的类型   “可解码”

第三次错误

  

NewsData.news.forEach {//闭包参数列表的上下文类型   需要1个参数,不能隐式忽略该参数插入'_ in'   //实例成员'news'不能用于类型'NewsData'

     

print(NewsData.news.title)//实例成员“ news”不能用于类型“ NewsData”,并且类型“ [articalData]”的值没有成员“ title”
  }

我知道我要实现的目标与现在播放的JSON不同,但是它们的格式几乎相同。任何建议都将受到欢迎。

2 个答案:

答案 0 :(得分:1)

第一个和第二个错误是由错字引起的(Marina的答案中已经提到)。

是两次Decodable。并且请用首字母大写来命名结构

struct NewsData: Decodable {
    let news: [ArticleData]
}

struct ArticleData: Decodable {
    let title: String
}

第三个错误实际上是两个错误。您必须在实例forEach上调用news(而不是类型News),并且必须在闭包中使用参数。

我重命名了一些变量以避免混淆

guard let data = data else { return }
do {
    let newsData = try JSONDecoder().decode(NewsData.self, from: data)
    newsData.news.forEach { item in
         print(item.title)                           
    }

或更短的速记参数名称语法

newsData.news.forEach { print($0.title) }

阅读错误消息。其中大多数内容非常清晰和具有描述性。

答案 1 :(得分:0)

您的结构类型上有一个错字,针对您的第二个错误进行了解释:

  

struct articalData:Decodeable {//使用未声明的类型'Decodeable'

这应该是:

struct NewsData: Decodable{
    let news: [articalData]
}

struct articalData: Decodable{
    let title: String
}