繁体   English   中英

Swift 4可解码的json数组

[英]Swift 4 decodable json arrays

所以我现在正在研究一个解析底部链接的JSON Api的程序

当我运行代码时,我得到一些输出,但不是全部 控制台输出的图像

主要是为可选类型的动漫,这是好的,因为它表明它的工作,但我也想访问名称和发布日期和语言,但我不知道如何在swift 4中使用这样的JSON数组。我会附上我的目前的代码如下。

import UIKit

struct AnimeJsonStuff: Decodable {
    let data: [AnimeDataArray]
}

struct AnimeDataArray: Decodable {
    let type: String?
}

class OsuHomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    func jsonDecoding() {
        let jsonUrlString = "https://kitsu.io/api/edge/anime"

        guard let url = URL(string: jsonUrlString) else {return} //
        URLSession.shared.dataTask(with: url) { (data, response, err) in

            guard let data = data else {return}

            do {
                let animeJsonStuff =  try JSONDecoder().decode(AnimeJsonStuff.self, from: data)
                print(animeJsonStuff.data)

                let animeDataArray = try JSONDecoder().decode(AnimeDataArray.self, from: data)
                print(animeDataArray.type as Any)
            } catch let jsonErr {
                print("Error serializing json", jsonErr)
            }
        }.resume()
    }
}

之后我有更多的代码,但它只是用于设置自定义collectionViewCells。

这里也是api的链接

标题为“Swift 4 decodable json arrays”的回答

let decoder = JSONDecoder()
do {
   let array = try decoder.decode([YouCodableStruct].self, from: response.data!)
   debugPrint(array)
} catch {
   debugPrint("Error occurred")
}

http://andrewmarinov.com/parsing-json-swift-4/

请检查以下内容:

我不会为所有钥匙添加。 我添加了一些attributes

struct AnimeJsonStuff: Decodable {
    let data: [AnimeDataArray]
}

struct AnimeLinks: Codable {
    var selfStr   : String?

    private enum CodingKeys : String, CodingKey {
        case selfStr     = "self"
    }
}
struct AnimeAttributes: Codable {
    var createdAt   : String?

    private enum CodingKeys : String, CodingKey {
        case createdAt     = "createdAt"
    }
}
struct AnimeRelationships: Codable {
    var links   : AnimeRelationshipsLinks?

    private enum CodingKeys : String, CodingKey {
        case links     = "links"
    }
}

struct AnimeRelationshipsLinks: Codable {
    var selfStr   : String?
    var related   : String?

    private enum CodingKeys : String, CodingKey {
        case selfStr     = "self"
        case related     = "related"
    }
}

struct AnimeDataArray: Codable {
    let id: String?
    let type: String?
    let links: AnimeLinks?
    let attributes: AnimeAttributes?
    let relationships: [String: AnimeRelationships]?

    private enum CodingKeys: String, CodingKey {
        case id = "id"
        case type = "type"
        case links = "links"
        case attributes = "attributes"
        case relationships = "relationships"
    }
}

Json解析:

func jsonDecoding() {
    let jsonUrlString = "https://kitsu.io/api/edge/anime"

    guard let url = URL(string: jsonUrlString) else {return} //
    URLSession.shared.dataTask(with: url) { (data, response, err) in
        guard let data = data else {return}
        do {
            let animeJsonStuff =  try JSONDecoder().decode(AnimeJsonStuff.self, from: data)
            for anime in animeJsonStuff.data {
                print(anime.id)
                print(anime.type)
                print(anime.links?.selfStr)
                print(anime.attributes?.createdAt)
                for (key, value) in anime.relationships! {
                    print(key)
                    print(value.links?.selfStr)
                    print(value.links?.related)
                }
            }
        } catch let jsonErr {
            print("Error serializing json", jsonErr)
        }
    }.resume()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM