Question: [Swift iOS APP] I would like to parse the JSON data about recipe, I have tried the following code but it still could not work.

[Swift iOS APP]

I would like to parse the JSON data about recipe, I have tried the following code but it still could not work. How could I modify the code or any other suitable JSON recipe data to parse? Thank you.

import UIKit

import CoreData

class DisciverTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {

private let gitRecipeURL = "https://raw.githubusercontent.com/LeaVerou/forkgasm/master/recipes.json"

private var recipes = [Recipe]()

override func viewDidLoad() {

super.viewDidLoad()

tableView.estimatedRowHeight = 92.0

tableView.rowHeight = UITableViewAutomaticDimension

getLatestRecipes()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {

// Return the number of sections

return 1

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

// Return the number of rows

return recipes.count

}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! DiscoverTableViewCell

// Configure the cell...

cell.nameLabel.text = recipes[indexPath.row].name

cell.typeLabel.text = recipes[indexPath.row].type

return cell

}

// MARK: - Helper methods

func getLatestRecipes() {

guard let recipeUrl = URL(string: gitRecipeURL) else {

return

}

let request = URLRequest(url: recipeUrl)

let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in

if let error = error {

print(error)

return

}

// Parse JSON data

if let data = data {

self.recipes = self.parseJsonData(data: data)

// Reload table view

OperationQueue.main.addOperation({

self.tableView.reloadData()

})

}

})

task.resume()

}

func parseJsonData(data: Data) -> [Recipe] {

var recipes = [Recipe]()

do {

let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary

// Parse JSON data

let jsonRecipes = jsonResult?["recipes"] as! [AnyObject]

for jsonRecipe in jsonRecipes {

var recipe = Recipe()

/*recipe.name = jsonRecipe["name"] as! String

recipe.type = jsonRecipe["description"] as! String*/

let name = jsonRecipe["recipe"] as! [String: AnyObject]

recipe.name = name["name"] as! String

recipes.append(recipe)

}

} catch {

print(error)

}

return recipes

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!