I have a Json file that lists books sorted in categories.
{
"FullBookList": [
{
"Fantasy": [
{
"title": "Alice's Adventures in Wonderland",
"author": "Lewis Carrol"
}
],
"Fiction": [
{
"Title": "Hamlet",
"Author": "Maggie O'Farrel"
}
],
"Comedy": [
{
"Title": "Good Omans",
"Author": "Neil Gaiman"
}
]
}
]
}
I'm trying to make swiftui create a list the "FullBookList' array so it lists...
- Fantasy >
- Fiction >
- Comedy >
Any help? I hope I've explained clearly. import Foundation
struct BookCategory: Decodable {
let FullBookList: [FullBookList]
}
struct FullBookList: Decodable {
let Fantasy: [Books]
}
struct Books: Decodable {
let title: String
let author: String
}
class BookJson: ObservableObject {
func GetBookList() {
do {
let json = Bundle.main.url(forResource: "Books", withExtension: "json")
let jsonData = try Data(contentsOf: json!)
let book = try! JSONDecoder().decode(BookCategory.self, from: jsonData)
DispatchQueue.main.async {
print(book.FullBookList) //???
}
} catch {
print("oof")
}
}
}