iOS 中的 Alamofire 正在接收字典而不是数组

Alamofire in iOS is Receiving Dictionary Instead of Array

我正在 iOS 中创建表格,并且应该以 JSON 数组的形式接收所需的数据。但是,当我在我的 iOS 应用程序中获取数据时,它显示为未排序的字典。我能够 运行 Postman 中的 GET 请求并正确接收数据,但是当我在我的 iOS 应用程序中通过 Alamofire 接收它时,它的格式不正确。 Alamofire 是否有可能以某种方式重新格式化 JSON 并将所有数组转换为字典,我可以以某种方式覆盖该设置吗?

这是 Postman 中 JSON 的列部分的示例:

这是我通过 Alamofire 收到的信息:

这就是我尝试访问 JSON

的方式
if let jsonColumns = json["columns"] as? [[String:Any]] {
        for columns in jsonColumns {
            for column in columns.values {
                if let c = column as? [String:Any] {
                    if c["isVisible"] as? Bool == false {
                        continue
                    }
                    if let columnName = c["name"] as? String {
                        dataSet.columns.append(columnName)
                    }
                }
            }
        }
    }

两个图像中 JSON 的结构相同,您有 columns 这是一个只有一个元素的字典数组 ([[String: Any]])。

字典中的值未排序,因为它们是通过键访问的,而不是通过值循环访问的。

来自here

A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order. You use a dictionary when you need to look up values based on their identifier, in much the same way that a real-world dictionary is used to look up the definition for a particular word.

Alamofire ObjectMapper 按字母顺序排列参数。

您可以将 JSON 更改为列数组,如下所示:

 { "columns" : [ 
     {"name": "1", "visible": false},
     {"name": "2", "visible": false},
     {"name": "3", "visible": true}
]}

这样就解决了排序问题