ios - 使用swiftyjson库swift的嵌套Json数据获取和追加问题

标签 ios json swift swifty-json

使用 swiftjson 库 swift 在嵌套的 json 中获取数据追加问题。

我创建了两个结构,但在附加最终列表时出现错误。追加数据时获取错误。我是否很好地创建了结构。

我的结构

struct GistModel {
    var comments : Int!
    var commentsUrl : String!
    var descriptionField : String!
    var owner : Owner!
}
struct Owner{
    var login : String!
}

JSON 数据结果:

{
            url: "https://api.github.com/gists/7e624eed62b3a317541791d719dcacf2",
            forks_url: "https://api.github.com/gists/7e624eed62b3a317541791d719dcacf2/forks",
            commits_url: "https://api.github.com/gists/7e624eed62b3a317541791d719dcacf2/commits",
            id: "7e624eed62b3a317541791d719dcacf2",
            node_id: "MDQ6R2lzdDdlNjI0ZWVkNjJiM2EzMTc1NDE3OTFkNzE5ZGNhY2Yy",
            git_pull_url: "https://gist.github.com/7e624eed62b3a317541791d719dcacf2.git",
            git_push_url: "https://gist.github.com/7e624eed62b3a317541791d719dcacf2.git",
            html_url: "https://gist.github.com/7e624eed62b3a317541791d719dcacf2",
            files: 
        {
            GistTest2: 
        {
            filename: "GistTest2",
            type: "text/plain",
            language: null,
            raw_url: "https://gist.githubusercontent.com/MasamMahmood/7e624eed62b3a317541791d719dcacf2/raw/7302f0d923e9e08b0e502ad9df762a1b2aa072e1/GistTest2",
            size: 29
            }
            },
            public: true,
            created_at: "2019-02-01T18:41:39Z",
            updated_at: "2019-02-01T19:01:16Z",
            description: "Gist Test 2",
            comments: 0,
            user: null,
            comments_url: "https://api.github.com/gists/7e624eed62b3a317541791d719dcacf2/comments",
            owner: 
        {
            login: "MasamMahmood",
            id: 36441313,
            node_id: "MDQ6VXNlcjM2NDQxMzEz",
            avatar_url: "https://avatars3.githubusercontent.com/u/36441313?v=4",
            gravatar_id: "",
            url: "https://api.github.com/users/MasamMahmood",
            html_url: "https://github.com/MasamMahmood",
            followers_url: "https://api.github.com/users/MasamMahmood/followers",
            following_url: "https://api.github.com/users/MasamMahmood/following{/other_user}",
            gists_url: "https://api.github.com/users/MasamMahmood/gists{/gist_id}",
            starred_url: "https://api.github.com/users/MasamMahmood/starred{/owner}{/repo}",
            subscriptions_url: "https://api.github.com/users/MasamMahmood/subscriptions",
            organizations_url: "https://api.github.com/users/MasamMahmood/orgs",
            repos_url: "https://api.github.com/users/MasamMahmood/repos",
            events_url: "https://api.github.com/users/MasamMahmood/events{/privacy}",
            received_events_url: "https://api.github.com/users/MasamMahmood/received_events",
            type: "User",
            site_admin: false
            },
            truncated: false
    }

Swift:

    switch response.result{
                    case .success(let value):
                        let json = JSON(value)
                        print(json)



                        for subJson in json.arrayValue {
                            let comm = subJson["comments"].intValue
                            let commurl = subJson["comments_url"].stringValue
                            let desc = subJson["description"].string
                            //let age = subJson["owner"]["login"].string

                            for item in subJson{
                           let login = subJson["owner"]["login"].string
                            // do something

                            }
                            let user = GistModel(comments: comm, commentsUrl: commurl, descriptionField: desc, login: login)//, owner: login)

                            self.DataList.append(user)
                            print(user)
                        }

我是新手追加列表时出错。 “使用未解析的标识符‘登录’”。

最佳答案

如果您愿意使用 Codable 转移到标准 json 处理,那么这将起作用。首先让结构实现Decodable

struct GistModel: Decodable {
    let comments: Int
    let commentsUrl: String
    let description: String //Changed the name here
    let owner: Owner
}

struct Owner: Decodable {
    let login: String
}

编码就是这样完成的

do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let result = try decoder.decode([GistModel].self, from: data)
    print(result[0].owner.login)
    print(result[0].comments)
    print(result[0].commentsUrl)
} catch {
    print(error)
}

关于ios - 使用swiftyjson库swift的嵌套Json数据获取和追加问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54637299/

相关文章:

ios - 滚动时快速更改 UICollectionView 高度

ios - 循环处理时间太长

javascript - JSON 成功时用字符串回复函数

android - 我应该如何将 json 放在 GET 请求中?

ios - 是否可以使用 Xcode 构建脚本将 JSON 文件下载到应用程序包中?

ios - 从自定义 UitableViewCell 中保存 textField.text

ios - 如何在不改变色彩空间的情况下获得 UIImage 负色

ios - 用于自定义 UIView 的 IBOutlets

ios - UITableView 中的单元格只有在 iOS 中选择后才会更新(快速)

swift 2.0 : Protocol extensions: Two protocols with the same function signature compile error