将Gloss JSON转换为swift 3.0中的数据

时间:2017-03-22 09:38:40

标签: json swift alamofire

我正在努力将Gloss JSON对象转换为swift 3.0中的Data。我想发送多部分请求API与图像和一些json有效负载。我是swift和IOS的新手,所以回答这个问题可能很明显。

我有一个这样的课程:

import Gloss

struct RepoOwner: Decodable {

    let ownerId: Int?
    let username: String?

    // MARK: - Deserialization

    init?(json: JSON) {
        self.ownerId = "id" <~~ json
        self.username = "login" <~~ json
    }

    func toJSON() -> JSON? {
        return jsonify([
            "id" ~~> self.ownerId,
            "login" ~~> self.username
        ])
    }

}

然后我想发送一个这样的多部分请求:

Alamofire.upload(
                multipartFormData: { multipartFormData in
                    if let repoOwner = self.repoOwner, let json = repoOwner.toJSON() {

                        data = // TODO: Help me!
                        multipartFormData.append(data, withName: "payload")
                        multipartFormData.append(self.imageData, withName: "image")
                    }
                },
                to: "https://myapi.com/endpoint",
                encodingCompletion: { ... }
                }
        )

有人能帮助我吗?

0 个答案:

没有答案