有什么方法可以将数据发送为对象而不是Alamofire中的参数?

时间:2019-01-19 15:25:00

标签: json swift parameters alamofire

我要发送这样的数据:

{ "userName" : username,"password" : password,"city" : [ {"name":name, "address":address ,"lat":lat } , {"name":name ,"address":address ,"lat":lat }]}

但是alamofire只接受像这样的参数:

[ "userName" : username, "password" : password,"city" : [ ["name":name, "address":address ,"lat":lat ] , ["name":name ,"address":address ,"lat":lat ]]]

即使我发送如下数据

[ "userName" : username, "password" : password, "city" : [{"name":name, "address":address ,"lat":lat } , {"name":name ,"address":address ,"lat":lat }]]

,它不接受JSON类型作为我数据中的城市值! 我收到错误消息:原因:'Invalid type in JSON write (_SwiftValue)' 有谁知道解决方案。

4 个答案:

答案 0 :(得分:0)

只需将参数放入字典中,并将alamofire请求方法中的编码设置为JsonEncoding.default

答案 1 :(得分:0)

您可以通过将alcofire传递给参数参数来发送可编码对象,例如:https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#request-parameters-and-parameter-encoders

因此您可以创建一个描述数据的快速结构:

struct Something: Codable {
    let userName: String
    let password: String
    let city: [City]
}

struct City: Codable {
    let name: String
    let address: String
    let lat: String
}

然后使用您的数据创建类似的对象以通过alamofire发送:

let encodableObject = Something(userName: userName, password: password, city: [City(name: name, address: address, lat: lat)])

AF.request("www.someApiEndpoint.com", method: method, parameters: encodableObject, encoder: JSONParameterEncoder.prettyPrinted, headers: headers)

答案 2 :(得分:0)

试试这个扩展。

创建一个应用了可编码协议的对象。

struct JSON {
    static let encoder = JSONEncoder()
}

extension Encodable {
    
    subscript(key: String) -> Any? {
        return dictionary[key]
    }
    
    // Convert an Encodable Object into dictionary
    var dictionary: [String: Any] {
        get {
            return (try? JSONSerialization.jsonObject(with: JSON.encoder.encode(self))) as? [String: Any] ?? [:]
        }
    }
    
}

创建您的可编码对象并使用 dictionary getter 这样它会自动为您返回字典。这是请求函数的示例。

func postWithHeader<T>(url : String, params : T?, onCompletion : @escaping (Data?, NetworkError?)->Void) where T : Encodable {
        print("----------> URL : \(url)")
        print("            Params  : \(String(describing: params?.dictionary ?? nil))")

        session.request(url, method: .post, parameters: (params).dictionary, encoding: JSONEncoding.default, headers: getHeader()).responseData { response in
            print("<---------- URL : \(url)")
            NetworkManager.printData(response: response)
    }
}

好吧,它不会发送整个对象,因为 alamofire 不允许。所以我们必须即兴发挥一点???

答案 3 :(得分:0)

struct Login: Encodable {
    let email: String
    let password: String
}

let login = Login(email: "test@test.test", password: "testPassword")

AF.request("https://httpbin.org/post",
           method: .post,
           parameters: login,
           encoder: JSONParameterEncoder.default).response { response in
    debugPrint(response)
}

您可以在 Alamofire GitHub 存储库中找到其他示例:https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#request-parameters-and-parameter-encoders