简体   繁体   English

Swift将JSON解析为表格视图

[英]Swift parse json into Table View

I can't seem to figure out how to parse my json into my table view, I'm still fairly new to Swift and iOS development, so some stuff is unfamiliar to me. 我似乎无法弄清楚如何将json解析为表格视图,对于Swift和iOS开发我还是很陌生,所以有些东西对我来说是陌生的。

I followed this guide: https://www.hackingwithswift.com/read/7/1/setting-up got it working perfectly, I just can't figure out how to properly parse my json into the Table View. 我遵循了该指南: https : //www.hackingwithswift.com/read/7/1/setting-up使其正常工作,我只是想不通如何正确地将json解析为Table View。 I'm getting my json from a url/api and successfully able to print it into the console. 我从url / api获取我的json,并成功将其打印到控制台中。

An example of my json is: 我的json的示例是:

{
"unackd": [
{
    "notification": {
        "title": "Title Test Number 200",
        "body": "passage local they water difficulty tank industry allow increase itself captured strike immediately type phrase driver change save potatoes stems addition behavior grain trap rapidly love refused way television bright 1100"
    },
    "data": {
        "id": "1100",
        "phone": "+15555551234"
    }
},
{
    "notification": {
        "title": "Title Test Number 199",
        "body": "announced beside well noted mysterious farm he essential likely deeply vast touch 1099"
    },
    "data": {
        "id": "1099",
        "phone": "+15555551234"
    }
}
],
"ackd": [
{
    "notification": {
        "title": "Title Test Number 200",
        "body": "passage local they water difficulty tank industry allow increase itself captured strike immediately type phrase driver change save potatoes stems addition behavior grain trap rapidly love refused way television bright 1100"
    },
    "data": {
        "id": "1100",
        "phone": "+15555551234"
    }
},
{
    "notification": {
        "title": "Title Test Number 199",
        "body": "announced beside well noted mysterious farm he essential likely deeply vast touch 1099"
    },
    "data": {
        "id": "1099",
        "phone": "+15555551234"
    }
}
]
}

I got these structs from: https://app.quicktype.io/?l=swift 我从以下网址获得了这些结构: https//app.quicktype.io/? l = swift

import Foundation

struct JSONSource: Codable {
    let unackd: [Unackd]
}

struct Unackd: Codable {
    let notification: Notification
    let data: DataClass
}

struct DataClass: Codable {
    let id, phone: String
}

struct Notification: Codable {
    let title, body: String
}

So my question is, how can I parse "title" and "body" from "notification" in the unackd array in each table row cell? 所以我的问题是,如何在每个表行单元格的未确认数组中解析“通知”中的“标题”和“正文”?

Thanks for any help! 谢谢你的帮助!

You can then do 然后你可以做

var notificationArr = [Notification]()

do {
  let res = try JSONDecoder().decode(JSONSource.self,from:data)
   notificationArr = res.unackd.map { $0.notification }
 }
 catch {
   print(error)
 }

After that you can esily use notificationArr as the table dataSource array 之后,您可以轻松地将notificationArr用作表dataSource数组

I think, Ackd and Unackd arrays have equal types inside. 我认为, AckdUnackd数组内部具有相同的类型。 So, i guess, it should be something like this: 所以,我想应该是这样的:

    var ackd:[Notification] = [Notification]()
    var unackd:[Notification] = [Notification]()

    func <your_func>(data:Data)->([Notification],[Notification])?{
         guard let res = try? JSONDecoder().decode(JSONSource.self,from:data) 
         else{ 
          return nil 
         }
         ackd = res["ackd"]
         unackd = res["unackd"]
         return (ackd, unackd)
    }

and in your tableView delegates 并在您的tableView委托中

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueCell(yourCell.self, indexPath: indexPath)
    let notif = unackd[indexPath.row]
    (cell as? yourCell)?.title.text = notif.title
    return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM