【发布时间】:2021-08-24 12:48:02
【问题描述】:
我有一个 Json 文件,其中列出了按类别排序的书籍。
{
"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"
}
]
}
]
}
我正在尝试让 swiftui 创建一个“FullBookList”数组列表,以便它列出...
- 奇幻>
- 小说>
- 喜剧 >
有什么帮助吗?我希望我已经解释清楚了。 导入基金会
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")
}
}
}
【问题讨论】:
-
请出示您目前拥有的代码。
-
向问题添加代码