如何在表视图上加载该JSON?

时间:2018-12-21 15:36:37

标签: ios json swift swift4 codable

我在解析json到collectionview时遇到问题 那是我的JSON:

{
"statusCode": 200,
"message": "Список курсов",
"content": [
    {
        "id": 1,
        "slug": "kurs-a1",
        "title": "Курс А1",
        "can_access": 1,
        "passed": 1,
        "sections": [
            {
                "id": 1,
                "slug": "razdel-1",
                "title": "Раздел 1",
                "can_access": 1,
                "passed": 1,
                "lessons": [
                    {
                        "id": 1,
                        "position": 1,
                        "title": "Сәлемдесу",
                        "slug": "salemdesu",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 1,
                        "passed": 1
                    },
                    {
                        "id": 3,
                        "position": 2,
                        "title": "Танысу",
                        "slug": "tanysu",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 1,
                        "passed": 1
                    }
                ]
            },
            {
                "id": 2,
                "slug": "razdel-2",
                "title": "Раздел 2",
                "can_access": 1,
                "passed": 0,
                "lessons": [
                    {
                        "id": 4,
                        "position": 3,
                        "title": "Бұл – менің отбасым",
                        "slug": "bul-menin-otbasym",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 1,
                        "passed": 0
                    },
                    {
                        "id": 5,
                        "position": 4,
                        "title": "Жасы нешеде?",
                        "slug": "zhasy-neshede",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 1,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 3,
                "slug": "razdel-3",
                "title": "Раздел 3",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 7,
                        "position": 5,
                        "title": "Туған күн",
                        "slug": "tugan-kun",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 9,
                        "position": 6,
                        "title": "Құттықтау",
                        "slug": "kuttyktau",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 4,
                "slug": "razdel-4",
                "title": "Раздел 4",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 10,
                        "position": 7,
                        "title": "Адам келбеті",
                        "slug": "adam-kelbeti",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 12,
                        "position": 8,
                        "title": "Адамның мінез-құлқы",
                        "slug": "adamnyn-minez-kulky",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 5,
                "slug": "razdel-5",
                "title": "Раздел 5",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 13,
                        "position": 9,
                        "title": "Менің мамандығым",
                        "slug": "menin-mamandygym",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 15,
                        "position": 10,
                        "title": "Кім болып жұмыс істейді?",
                        "slug": "kim-bolyp-zhumys-istejdi",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 6,
                "slug": "razdel-6",
                "title": "Раздел 6",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 16,
                        "position": 11,
                        "title": "Жұмыс орны",
                        "slug": "zhumys-orny",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 17,
                        "position": 12,
                        "title": "Жұмыс күні",
                        "slug": "zhumys-kuni",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 7,
                "slug": "razdel-7",
                "title": "Раздел 7",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 19,
                        "position": 13,
                        "title": "Тесты ЦОР-а",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 20,
                        "position": 14,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 8,
                "slug": "razdel-8",
                "title": "Раздел 8",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 22,
                        "position": 15,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 23,
                        "position": 16,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 9,
                "slug": "razdel-9",
                "title": "Раздел 9",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 25,
                        "position": 17,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 27,
                        "position": 18,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 10,
                "slug": "razdel-10",
                "title": "Раздел 10",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 28,
                        "position": 19,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 29,
                        "position": 20,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 11,
                "slug": "razdel-11",
                "title": "Раздел 11",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 32,
                        "position": 21,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 33,
                        "position": 22,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            },
            {
                "id": 12,
                "slug": "razdel-12",
                "title": "Раздел 12",
                "can_access": 0,
                "passed": 0,
                "lessons": [
                    {
                        "id": 34,
                        "position": 23,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    },
                    {
                        "id": 35,
                        "position": 24,
                        "title": "Тесты",
                        "slug": "testy",
                        "image": "https://tilqural.kz/assets/img/default-lesson-icon.png",
                        "can_access": 0,
                        "passed": 0
                    }
                ]
            }
        ]
    }
]
}

我将其解码,并且希望将其解析为collectionview。
如何获取内容[{sections [{课程[{title}]}]}]]?
我提出了请求

 func numberOfSections(in collectionView: UICollectionView) -> Int {
    return arrData1.count
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let dn = arrData1[section]
    let st = dn.sections?[section]
    return (st?.lessons?.count)!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "main_cell", for: indexPath) as! MainCollectionViewCell

    cell.lesson_text_1.text = arrData1.first?.sections?[indexPath.item].title

//        cell.lesson_img_1.image = UIImage(named: lesson_image_1[indexPath.row])

// cell.lesson_text_1.text = lesson_text_1 [indexPath.row]         返回单元格

}

但它向我返回了2个单元格 但是我需要24节课! 谢谢

解码

 struct Welcome: Codable {
    let statusCode: Int
    let message: String
    let content: [Content]
}

struct Content: Codable {
    let id: Int
    let slug, title: String
    let canAccess: Int?
    let passed: Int
    let sections: [Content]?
    let lessons: [Lesson]?

    enum CodingKeys: String, CodingKey {
        case id, slug, title
        case canAccess = "can_access"
        case passed, sections
        case lessons = "lessons"
    }
}

struct Lesson: Codable {
    let id, position: Int
    let title, slug: String
    let image: String
    let canAccess: Int?
    let passed: Int

    enum CodingKeys: String, CodingKey {
        case id, position, title, slug, image
        case canAccess = "can_access"
        case passed
    }
}

api调用功能

let mData = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
        guard let data = data else { return }
        do {
            let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            let data_new = try decoder.decode(Welcome.self, from: data)

            DispatchQueue.main.async {
                self.tableview.reloadData()
                self.arrData1 = data_new.content

                print(data_new)
            }

        } catch {
            print("Error in CheckNew is : \n\(error)")
        }
    }
    mData.resume()  

enter image description here

1 个答案:

答案 0 :(得分:0)

尽管JSON结构非常相似,但我建议对SectionLesson使用单独的结构。

另一个好处是您摆脱了所有可选项

struct Welcome: Codable {
    let statusCode: Int
    let message: String
    let content: [Content]
}

struct Content: Codable {
    let id: Int
    let slug, title: String
    let canAccess: Int
    let passed: Int
    let sections: [Section]
}

struct Section: Codable {
    let id: Int
    let title, slug: String
    let canAccess: Int
    let passed: Int
    let lessons: [Lesson]
}

struct Lesson: Codable {
    let id, position: Int
    let title, slug: String
    let image: URL
    let canAccess: Int
    let passed: Int
}

在使用.convertFromSnakeCase策略时,所有编码键都是多余的。