Couldn't parse json file as array

Hello Everyone

I'm using Xcode 13 I'm new to SwiftUI so the I'm learning SwiftUI from Apple document "Introducing SwiftUI" and I do exactly like the introduction

but when it comes to the LandmarkRow part, after Step 4, when i click Preview it says that Couldn't parse landmarkData.json as Array

If anyone has already read that Document, can you help me solve this problem Thank you for your help

The page "Introducing SwiftUI" has several tutorials and each tutorial has several sections. So, there are many Step 4s. Can you clarify in which tutorial and in which section you get that message?

Is it this part ?

Step 3

Add landmark as a stored property of LandmarkRow. When you add the landmark property, the preview stops working, because the LandmarkRow type needs a landmark instance during initialization.

Then did you do this for step 4 ?

To fix the preview, you’ll need to modify the preview provider.

Step 4

In the previews static property of LandmarkRow_Previews, add the landmark parameter to the LandmarkRow initializer, specifying the first element of the landmarks array.

The preview displays the text, “Hello, World!”.

You should have somewhere:

final class ModelData: ObservableObject {
    @Published var landmarks: [Landmark] = load("landmarkData.json")   // Donc T est [Landmark]
}

func load<T: Decodable>(_ filename: String) -> T {

    let data: Data
    
    guard let file = Bundle.main.url(forResource: filename, withExtension: nil) else {
        fatalError("Couldn't find \(filename) in main Bundle")
    }
    
    do {
        data = try Data(contentsOf: file)
    } catch {
        fatalError("Couldn't load \(filename) in main Bundle:\n\(error)")
    }
    
    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
    }
}

Your exact message is : Couldn't parse landmarkData.json as Array You should have another line describing the error: please post it.

So you probably have a problem in the landmarkData.json file. Could you attach this file as well ?

Here is what it should be:

It says:

I code exactly like yours

And the json file i downloaded directly from the introdction

Thanks for showing the details.

So, it's Building Lists and Navigation, Section 2 Create the Row View, Step 4, OK?

You may need to go back to Section 1 Create a Landmark Model and check if your Landmark.swift is correct:

import Foundation
import SwiftUI
import CoreLocation

struct Landmark: Hashable, Codable {
    var id: Int
    var name: String
    var park: String
    var state: String
    var description: String

    private var imageName: String
    var image: Image {
        Image(imageName)
    }

    private var coordinates: Coordinates
    var locationCoordinate: CLLocationCoordinate2D {
        CLLocationCoordinate2D(
            latitude: coordinates.latitude,
            longitude: coordinates.longitude)
    }

    struct Coordinates: Hashable, Codable {
        var latitude: Double
        var longitude: Double //<-
    }
}

I already checked

but still

Couldn't parse json file as array
 
 
Q