Learn Courses My Dashboard

Lesson 12 module 2 JSon parsing pizzas

Hi, the challenge sounds simple but I do not know how to parse an array, error seem to prompt that the issue is how data represented this is my Json, thank you!

[
{
“name”:”Margarita ”,
  “toppings”: [“Mozarella”, “Tomato Sause“, “Olives”]
},
{
“name”:”Marinara”,
  “toppings”: [“Tuna”, “Ansjovis”, “Squid”]
},
{
“name”:”Florentina”,
  “toppings”: [“Egg”, “Mushrooms”, “Cheese”]
}
]

this is my model:

//
//  PizzaMenu.swift
//  pizzaMenu
//
//  Created by Ewa Boer on 01/05/2021.
//

import Foundation



class PizzaMenu: Identifiable, Decodable {
    
    var id:UUID?
    
    var name = ""
    var toppings = [String]()
    
    
}

and this is my MenuModel

//
//  MenuModel.swift
//  pizzaMenu
//
//  Created by Ewa Boer on 01/05/2021.
//

import Foundation


class MenuModel: ObservableObject {
    
    @Published var menu = [PizzaMenu]()
    
    init() {
        let pathString = Bundle.main.path(forResource: "data", ofType: "json")
        
        if let path = pathString {
            let url = URL(fileURLWithPath: path)
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                do {
                    let menuData = try decoder.decode([PizzaMenu].self, from: data)
                    for r in menuData {
                        r.id = UUID()
                    }
                    self.menu = menuData
                }
                catch {
                    print(error)
                }
            }
            catch {
                print(error)
            }
        }
    }
}

    
  

and this is my view:

//
//  ContentView.swift
//  pizzaMenu
//
//  Created by Ewa Boer on 01/05/2021.
//

import SwiftUI

struct ContentView: View {
    
    @ObservedObject var model = MenuModel()
    
    var body: some View {
        
        VStack{
            List(model.menu) {
                r in
                VStack {
                    Text(r.name)
                    Text(r.toppings)
                }
            }
            
            
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Hi Ewa,

In your ContentView the toppings is an array of items so you need to loop through them. Change you code to this:

struct ContentView: View {

    @ObservedObject var model = MenuViewModel()

    var body: some View {

        VStack{
            List(model.menu) {
                r in
                VStack {
                    Text(r.name)
                    ForEach(r.toppings, id:\.self) { topping in
                        Text(topping)
                    }
                }
            }
        }
    }
}

I’m sorry I still have this error and cant see anything in the preview or simulator

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 4." UserInfo={NSDebugDescription=No string key for value in object around character 4.})))

You have typographical quotes “” in your JSON instead of “straight” quotes "".

1 Like

ai! thank you !