在swift中使用alamofire将参数作为原始数据发送。

问题描述 投票:0回答:2

我有以下原始数据作为参数发送,当在swift中使用alamofire时。

{
    "customer": {
        "firstname": "test",
        "lastname": "user",
        "email": "[email protected]",
        "website_id": 1,
        "addresses": [
        {
            "customer_id": 3292,
            "region": {
                "region": "New York"
            },
            "country_id": "US",
            "street": [
                "US"
            ],
            "telephone": "84656313",
            "postcode": "34521",
            "city": "us",
            "firstname": "test",
            "lastname": "usr",
            "default_shipping": true,
            "default_billing": true
        }
    ]

    }

}

我在代码中用alamofire写了下面给出的参数。

  let customer : Parameters = [
                    "email":email,
                    "firstname":fname,
                    "lastname":lname,
                    "website_id":1,
                    "addresses": {
                        [
                            "customer_id": id,
                            "region": [
                                "region": state
                            ],
                            "country_id": country,
                            "street": {
                                self.add1Txt.text! + self.add2Txt.text!
                            },
                            "telephone": self.phoneTxt.text!,
                            "postcode": self.pincodeTxt.text!,
                            "city": self.cityTxt.text!,
                            "firstname": self.fnameLbl.text!,
                            "lastname": self.lnameLbl.text!,
                            "default_shipping": true,
                            "default_billing": true
                        ]
                    }
                 ]

                let parameters: Parameters =
                               [
                                    "customer": customer

                               ]

它显示'Invalid type in JSON write (__SwiftValue)'。这个参数传递的问题是什么?

json swift alamofire raw-data
2个回答
0
投票

你的customer变量不是正确的字典。"addresses": { 实际上是一个字典的数组,它应该像下面的代码。除此以外,如果你用Codable创建模型结构,并借助JSONEncoder和JSONSerialization转换为字典,就可以避免这种问题,你可以看到这样的内容 .

 let parameters: Parameters = ["customer": [
            "addresses" : [[

                            "city": "us",
                            "country_id" :"US",
                            "customer_id" :"3292",
                            "default_billing" : 1,
                            "default_shipping" : 1,
                            "firstname" :"test",
                            "lastname" :"usr",
                            "postcode" :34521,
                            "region" : ["region" : "New York"],
                            "street" : ["US"],
                            "telephone" : 84656313

            ]],
            "email" : "[email protected]",
            "firstname": "test",
            "lastname": "user",
            "website_id" : 1
        ]]

0
投票

使用 快速型 从你的json中即时生成结构。

import Foundation

// MARK: - Parameters
struct Parameters: Codable {
    let customer: Customer?
}

// MARK: - Customer
struct Customer: Codable {
    let firstname, lastname, email: String?
    let websiteID: Int?
    let addresses: [Address]?

    enum CodingKeys: String, CodingKey {
        case firstname, lastname, email
        case websiteID = "website_id"
        case addresses
    }
}

// MARK: - Address
struct Address: Codable {
    let customerID: Int?
    let region: Region?
    let countryID: String?
    let street: [String]?
    let telephone, postcode, city, firstname: String?
    let lastname: String?
    let defaultShipping, defaultBilling: Bool?

    enum CodingKeys: String, CodingKey {
        case customerID = "customer_id"
        case region
        case countryID = "country_id"
        case street, telephone, postcode, city, firstname, lastname
        case defaultShipping = "default_shipping"
        case defaultBilling = "default_billing"
    }
}

// MARK: - Region
struct Region: Codable {
    let region: String?
}
© www.soinside.com 2019 - 2024. All rights reserved.