Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to parse the JSON with multiple structs? #99

Open
breakingbeats opened this issue Sep 11, 2020 · 0 comments
Open

How to parse the JSON with multiple structs? #99

breakingbeats opened this issue Sep 11, 2020 · 0 comments

Comments

@breakingbeats
Copy link

How can I parse the following Firebase Realtime Database JSON?

firebase data

let firebaseRef = Database.database().reference()
firebaseRef.observe(.value, with: { (snapshot) in

guard let value = snapshot.value as? [String: [String: [String: Any]]] else { return print("type wrong") }
    do {
        let model = try FirebaseDecoder().decode(AllData.self, from: Array(value.values))
        print("model:", model)
    } catch let error {
         print("error", error)
    }

})

...and the following structs...

struct AllData: Codable {
let news: News
let events: Events
let crew: Crew
let artists: Artists
}
///////////////////////////////////
struct News: Codable {
let news: NewsDetail
}

struct NewsDetail: Codable {
let title: String
let subtitle: String
}
////////////////////////////////////
struct Artists: Codable {
let artist: NewsDetail
}

struct ArtistDetail: Codable {
let name: String
let image: String
}
//////////////////////////////////
struct Crew: Codable {
let member: Member
}

struct Member: Codable {
let name: String
let image: String
}
////////////////////////////////////
struct Events: Codable {
let event: EventDetail
}
struct EventDetail: Codable {
let title: String
let subtitle: String
let date: String
}

Also with guard let value = snapshot.value as? [String:Any] gives me a typeMismatch(Swift.Dictionary<Swift.String, Any>) error. Can anyone help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant