J. Doe43
J. Doe43

Reputation: 207

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.

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. I'm getting my json from a url/api and successfully able to print it into the console.

An example of my json is:

{
"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

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!

Upvotes: 0

Views: 61

Answers (2)

AKovalev
AKovalev

Reputation: 61

I think, Ackd and Unackd arrays have equal types inside. 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

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
}

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

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

Upvotes: 1

Related Questions