【问题标题】:Codable with noisy JSON response [closed]可使用嘈杂的 JSON 响应进行编码 [关闭]
【发布时间】:2021-05-14 10:27:51
【问题描述】:

一般来说,我了解Codable 的工作原理。一个简单的例子是:

[
    {
        "id": 1,
        "name": "Paul",
        "photo": {
            "id": 48,
            "url": "https://..."
        }
    },
    {
        "id": 2,
        "name": "Andrew",
        "photo": {
            "id": 389,
            "url": "https://..."
        }
    }
]

这需要两个简单的结构:

struct User: Codable {
    var id: Int
    var name: String
    var photo: Photo
}

struct Photo: Codable {
    var id: Int
    var url: String
}

鉴于上面的例子,我的 API 的响应 json 格式更复杂:

{
    "data": [
        {
            "data": {
                "id": 1,
                "attributes": {
                    "name": "Paul"
                },
                "relationships": {
                    "photo": {
                        "data": {
                            "id": 48,
                            "attributes": {
                                "url": "https://..."
                            }
                        }
                    }
                }
            }
        },
        {
            "data": {
                "id": 2,
                "attributes": {
                    "name": "Andrew"
                },
                "relationships": {
                    "photo": {
                        "data": {
                            "id": 389,
                            "attributes": {
                                "url": "https://..."
                            }
                        }
                    }
                }
            }
        }
    ]
}

我已经使用SwiftyJSON 通过消除所有噪音并尝试使格式像第一个 json 示例那样来预解析/展平 json。但是,我觉得可能有更好、更明显的方法来做到这一点,并且操作 json 似乎很容易出错。

我已经阅读了 CodingKeys,但在这一点上,这几乎与使用 SwiftyJSON 手动解析 json 一样多。

有谁知道如何有效地做到这一点

【问题讨论】:

  • 您为什么要发布与问题无关的 json 和代码,以及该 json 的真正问题是什么?你当然不需要 CodingKey 枚举。
  • 我正在尝试提供上下文,仅此而已。而且我对 CodingKeys 不是很熟悉。问题是它过于复杂,我不知道如何解析它。
  • 在这里查看github.com/jatinfl15/Codable/blob/main/Codable.swift 我为您嘈杂的 json 创建了可编码结构。但请关注@JoakimDanielson 的评论。
  • 感谢分享@RB's!

标签: json swift codable


【解决方案1】:

使用quicktype.io 从指定的 JSON 为您生成响应模型。

import Foundation

// MARK: - Root
struct Root: Codable {
    let data: [Datum]
}

// MARK: - Datum
struct Datum: Codable {
    let data: DatumData
}

// MARK: - DatumData
struct DatumData: Codable {
    let id: Int
    let attributes: PurpleAttributes
    let relationships: Relationships
}

// MARK: - PurpleAttributes
struct PurpleAttributes: Codable {
    let name: String
}

// MARK: - Relationships
struct Relationships: Codable {
    let photo: Photo
}

// MARK: - Photo
struct Photo: Codable {
    let data: PhotoData
}

// MARK: - PhotoData
struct PhotoData: Codable {
    let id: Int
    let attributes: FluffyAttributes
}

// MARK: - FluffyAttributes
struct FluffyAttributes: Codable {
    let url: String
}

let root = try JSONDecoder().decode(Root.self, from: jsonData)

【讨论】:

  • 谢谢你,这有帮助!
  • @JoakimDanielson 不错,已更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
相关资源
最近更新 更多