Swift - ObjectMapper:映射 jsonString

发布于 2025-01-12 00:11:54 字数 1392 浏览 0 评论 0原文

我有两个模型:

class CellModel: StaticMappable {

static func objectForMapping(map: Map) -> BaseMappable? {
    return nil
}

func mapping(map: Map) {
    id <- map["id"]
    title <- map["title"]
    description <- map["description"]
}


private var id: Int
private var title: String
private var description: String

init(id: Int, title: String, description: String) {
    self.id = id
    self.title = title
    self.description = description
}

}

class CellModelArray: StaticMappable {

var cells = [CellModel]()

static func objectForMapping(map: Map) -> BaseMappable? {
    return nil
}

func mapping(map: Map) {
    cells <- map["cells"]
}

}

我从这样的对象创建了一个 jsonString:

let jsonString = Mapper().toJSONString(rootModel, prettyPrint: false)

json 看起来像这样:

{"cells":[{"id":0,"title":"Header","description":"Description"},{"description":"Description","id":0,"title":"Header"},{"description":"Description","id":1,"title":"Header"},{"id":0,"title":"Header","description":"Description"},{"description":"Description","title":"Header","id":0}]}

然后我想获取这个字符串并将其转换回对象,但是当我这样尝试时:

var cells = CellModelArray()

cells = Mapper<CellModelArray>().map(JSONString: code) ?? CellModelArray()

它不起作用并返回零。感谢您的帮助。

I have two models:

class CellModel: StaticMappable {

static func objectForMapping(map: Map) -> BaseMappable? {
    return nil
}

func mapping(map: Map) {
    id <- map["id"]
    title <- map["title"]
    description <- map["description"]
}


private var id: Int
private var title: String
private var description: String

init(id: Int, title: String, description: String) {
    self.id = id
    self.title = title
    self.description = description
}

}

and

class CellModelArray: StaticMappable {

var cells = [CellModel]()

static func objectForMapping(map: Map) -> BaseMappable? {
    return nil
}

func mapping(map: Map) {
    cells <- map["cells"]
}

}

I created a jsonString from object like this:

let jsonString = Mapper().toJSONString(rootModel, prettyPrint: false)

and json looks like this:

{"cells":[{"id":0,"title":"Header","description":"Description"},{"description":"Description","id":0,"title":"Header"},{"description":"Description","id":1,"title":"Header"},{"id":0,"title":"Header","description":"Description"},{"description":"Description","title":"Header","id":0}]}

Then I want to take this string and convert it back to the object, but when I try it like that:

var cells = CellModelArray()

cells = Mapper<CellModelArray>().map(JSONString: code) ?? CellModelArray()

it does not work and returns nil. Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2025-01-19 00:11:54

请删除ObjectMapper

它是一个优秀的库,但自 Swift 4(2017 年推出)以来,它已经过时,取而代之的是内置的、更快速 Codable 协议。

主要好处是模型文件可以是结构(值类型),并且更加可靠,因为开发人员不必关心文字字符串键

这就足够了

struct Model: Codable {
    let cells : [Cell]
}

struct Cell: Codable {
    let id: Int
    let title: String
    let description: String
}

可以使用此代码对给定的 JSON 字符串

let jsonString = """
{"cells":[{"id":0,"title":"Header","description":"Description"},{"description":"Description","id":0,"title":"Header"},{"description":"Description","id":1,"title":"Header"},{"id":0,"title":"Header","description":"Description"},{"description":"Description","title":"Header","id":0}]}
"""

进行解码和编码

do {
    // Decode the JSON
    let decoded = try JSONDecoder().decode(Model.self, from: Data(jsonString.utf8))
    print(decoded)

    // Encode it back
    let encoded = try JSONEncoder().encode(decoded)
    print(String(data: encoded, encoding: .utf8)!)
} catch {
    print(error)
}

Please drop ObjectMapper.

It's a library of merit but since Swift 4 (introduced 2017) it has become obsolete in favor of the built-in and swiftier Codable protocol.

The main benefit is that the model files can be structs (value types) and it's much more reliable because the developer doesn't have to take care of literal string keys

This is sufficient

struct Model: Codable {
    let cells : [Cell]
}

struct Cell: Codable {
    let id: Int
    let title: String
    let description: String
}

The given JSON string

let jsonString = """
{"cells":[{"id":0,"title":"Header","description":"Description"},{"description":"Description","id":0,"title":"Header"},{"description":"Description","id":1,"title":"Header"},{"id":0,"title":"Header","description":"Description"},{"description":"Description","title":"Header","id":0}]}
"""

can be decoded and encoded with this code

do {
    // Decode the JSON
    let decoded = try JSONDecoder().decode(Model.self, from: Data(jsonString.utf8))
    print(decoded)

    // Encode it back
    let encoded = try JSONEncoder().encode(decoded)
    print(String(data: encoded, encoding: .utf8)!)
} catch {
    print(error)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文