8

我必须通过 POST 请求发送字典数组。例如:

materials: [[String, String]] = [
  [
    "material_id": 1,
    "qty": 10
  ],
  [
    "material_id": 2,
    "qty": 5
  ]
]

Alamofire.request 发送下一个帖子数据:

materials => array(
  [0] => array("material_id" => 1),
  [1] => array("qty" => 10),
  [2] => array("material_id" => 2),
  [3] => array("qty" => 5),
)

我想收到那个表示:

materials => array(
  [0] => array(
    "material_id" => 1,
    "qty" => 10
  ),
  [1] => array(
    "material_id" => 2,
    "qty" => 5
  ),
)
4

3 回答 3

9

问题出在附加方法中。我在 PHP 上编码了 5 年,忘记了在 Swift 中索引不会像 PHP 那样自动分配。所以,我的第一个错误代码是:

func getParameters() -> [[String: AnyObject]] {
    var result = [[String: AnyObject]]()

    for mmap in mmaps {
        let material: [String: AnyObject] = [
            "material_id": mmap.material.id,
            "quantity": mmap.qty
        ]
        result.append(material)
    }

    return result
}

答案是根据需要硬分配键:

func getParameters() -> [String: [String: AnyObject]] {
    var result = [String: [String: AnyObject]]()

    let mmaps = self.mmaps.allObjects as [Mmap]
    for i in 0..<mmaps.count {
        let mmap = mmaps[i]
        let material: [String: AnyObject] = [
            "material_id": mmap.material.id,
            "quantity": mmap.qty
        ]
        result["\(i)"] = material
    }

    return result
}
于 2015-01-08T02:21:37.190 回答
2

几个想法:

  1. 如果您将响应作为带有一个键的字典发送,那将是最简单的,并且它将正确地对字典中的数组进行编码:

    let materials = [ "materials":
        [
            [
                "material_id": 1,
                "qty": 10
            ],
            [
                "material_id": 2,
                "qty": 5
            ]
        ]
    ]
    

    然后,您可以将其作为parametersof提供request(),Alamofire 将为您正确编码。

  2. 如果您想发送一系列字典,另一种方法是将 Web 服务更改为接受 JSON。然后,您可以自己对 JSON 进行编码(使用JSONSerializationJSONEncoder),设置请求正文,然后发送该请求。

  3. 如果要application/x-www-form-urlencoded使用字典数组发送请求,则必须自己对其进行编码。在 Swift 3 及更高版本中,这可能看起来像:

    func encodeParameters(_ object: Any, prefix: String? = nil) -> String {
        if let dictionary = object as? [String: Any] {
            return dictionary.map { key, value -> String in
                self.encodeParameters(value, prefix: prefix != nil ? "\(prefix!)[\(key)]" : key)
                }.joined(separator: "&")
        } else if let array = object as? [Any] {
            return array.enumerated().map { (index, value) -> String in
                return self.encodeParameters(value, prefix: prefix != nil ? "\(prefix!)[\(index)]" : "\(index)")
            }.joined(separator: "&")
        } else {
            let escapedValue = "\(object)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)!
            return prefix != nil ? "\(prefix!)=\(escapedValue)" : "\(escapedValue)"
        }
    }
    

    在哪里

    extension CharacterSet {
    
        /// Returns the character set for characters allowed in the individual parameters within a query URL component.
        ///
        /// The query component of a URL is the component immediately following a question mark (?).
        /// For example, in the URL `http://www.example.com/index.php?key1=value1#jumpLink`, the query
        /// component is `key1=value1`. The individual parameters of that query would be the key `key1`
        /// and its associated value `value1`.
        ///
        /// According to RFC 3986, the set of unreserved characters includes
        ///
        /// `ALPHA / DIGIT / "-" / "." / "_" / "~"`
        ///
        /// In section 3.4 of the RFC, it further recommends adding `/` and `?` to the list of unescaped characters
        /// for the sake of compatibility with some erroneous implementations, so this routine also allows those
        /// to pass unescaped.
    
        static var urlQueryValueAllowed: CharacterSet = {
            let generalDelimitersToEncode = ":#[]@"    // does not include "?" or "/" due to RFC 3986 - Section 3.4
            let subDelimitersToEncode = "!$&'()*+,;="
    
            var allowed = CharacterSet.urlQueryAllowed
            allowed.remove(charactersIn: generalDelimitersToEncode + subDelimitersToEncode)
            return allowed
        }()
    }
    

    显然,使用response适合服务器响应性质的任何方法(例如responsevs. responseJSONvs. ...)。

    无论如何,上面生成的请求正文如下所示:

    materials[0][material_id]=1&materials[0][qty]=10&materials[1][material_id]=2&materials[1][qty]=5
    

    这似乎是由您在问题中要求的服务器解析的。

值得注意的是,最后一点说明了application/x-www-form-urlencoded使用嵌套字典/数组结构的请求的准备,正如这里所设想的那样。这适用于我的主要 ISP 运行的服务器,但我必须承认我没有在正式的 RFC 中看到这个约定,所以我会小心翼翼地这样做。我个人倾向于将其实现为 JSON 接口。

有关 Swift 的早期版本,请参阅此答案的先前版本

于 2015-01-06T17:24:15.810 回答
0

您可以将您的数组作为 JSON 字符串并发布到服务器,然后在服务器端解析 JSON,然后您可以从中获取所需的数据,例如:

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: yourArry options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

希望这可以帮助.. :)

于 2015-01-06T08:50:50.540 回答