尝试使用 Alamofire 解析 Swift 3 中的 JSON

Trying to parse JSON in Swift 3 using Alamofire

我正在使用 Alamofire 调用我的 API 其中 returns 以下 JSON:

{
    outer =         {
        height = 3457;
        width = 2736;
    };
    cost = 220;
    name = "Mega";
},
    {
    outer =         {
        height = 275;
        width = 300;
    };
    cost = 362;
    name = "Ultra";
},

例如,我想获取姓名和身高并将它们存储到数组中。

这是我用来获取高度的相关代码:

 if let url = URL(string: "LINKTOAPI") {
        var urlRequest = URLRequest(url: url)
        urlRequest.httpMethod = HTTPMethod.get.rawValue
        urlRequest.addValue(apiKey, forHTTPHeaderField: "user-key")
        urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")

        Alamofire.request(urlRequest).responseJSON {response in
            if let result = response.result.value as? [String:Any],
                let main = result["outer"] as? [[String:String]]{
                for obj in main{
                    print(obj["height"])                       
                }
            }
 }

下面的另一个例子:

我使用上面的例子尝试相关代码:

 Alamofire.request(urlRequest).responseJSON {response in
            if let jsonDict = response.result.value as? [String:Any],
                let dataArray = jsonDict["outer"] as? [[String:Any]] {
                let nameArray = dataArray.flatMap { [=14=]["height"] as? String }
                print(nameArray)
            }

在这两个示例中,几乎没有打印任何内容,所以我不确定哪里出了问题。任何建议表示赞赏

编辑:完整 JSON 下面

SUCCESS: (
        {
    outer =         {
        height = 3457;
        width = 2736;
    };
    cost = 220;
    name = "Mega";
 },
        {
    outer =         {
        height = 275;
        width = 300;
    };
    cost = 362;
    name = "Ultra";
 },
        {
    outer =         {
        height = 31;
        width = 56;
    };
    cost = 42;
    name = "Mini";
 }
)

根据评论,我了解到结构实际上是这样的:

[{
    outer =         {
        height = 3457;
        width = 2736;
    };
    cost = 220;
    name = "Mega";
},
    {
    outer =         {
        height = 275;
        width = 300;
    };
    cost = 362;
    name = "Ultra";
}]

我仍然不完全理解你希望对所有这些变量做什么,但这是你如何解析它们以打印每个部分的 height 值,因为这就是你看起来的在示例中尝试做:

//Here you safely unwrap all of the sections as an array of dictionaries
if let allSections = response.result.value as? [[String : Any]] {

    //Here we will loop through the array and parse each dictionary
    for section in allSections {
        if let cost = section["cost"] as? Int, let name = section["name"] as? String, let outer = section["outer"] as? [String : Any], let height = outer["height"] as? Int, let width = outer["width"] as? Int {
            //Inside here you have access to every attribute in the section

            print(height)
        }
    }
}