【问题标题】:Is it possible to create a JSON using ObjectMapper?是否可以使用 ObjectMapper 创建 JSON?
【发布时间】:2018-05-09 13:54:50
【问题描述】:

我正在开发一个类似于亚马逊的电子商务应用程序,我需要创建一个带有字典数组的 JSON。 例如:

    {
    "products": [
        {
            "product_id":1,
            "quantity":2
        },
        {
            "product_id":2,
            "quantity":2
        }
    ]
}

ObjectMapper 框架可以吗?产品是动态生成的,而不是固定数量的产品。每次我们结帐时,产品列表都会根据用户的选择而有所不同。

【问题讨论】:

标签: ios json swift objectmapper


【解决方案1】:

直接回答您的问题.. 是的。项目homepage上是这么写的:

你可以这样做:

let JSONString = user.toJSONString(prettyPrint: true)

let JSONString = Mapper().toJSONString(user, prettyPrint: true)

对于 Swift 4,使用Codable 协议和JSONEncoder()。见Encoding and Decoding Custom Types

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2021-04-28
    相关资源
    最近更新 更多