1

所以我试图获取他的原始 json 数据并使用它最终在表格中查看(所以一个表格单元格将是 --> 阿联酋航空 - 1588.77 美元)

问题:解析 JSON 数据时遇到问题.. alamofire 显然是自动完成的吗?但我完全对数据类型感到困惑。我不断收到奇怪的错误,例如“没有一个名为 subscript 的成员”(我也安装了 swiftyjson,但一个非 swiftyjson 解决方案也应该可以工作。

代码:

    request(qpxRequest).responseJSON { (request, response, json, error) -> Void in
        if response != nil {
           //println(response!)
        }
        if json != nil {


          // 1. parse the JSON data into a Foundation object
         // 2. Grab the data from the foundation object (so its can be looped though      in a table)

}


{
trips =     {
    data =         {
        carrier =             (
                            {
                name = "Cathay Pacific Airways Ltd.";
            },
                            {
                name = Emirates;
            },
                            {
                name = "Ethiopian Airlines Enterprise";
            },
                            {
                name = "Qantas Airways Ltd.";
            },
                            {
                name = "South African Airways";
            }
        );
    };
    tripOption =         (
                    {
            saleTotal = "AUD1537.22";
        },
                    {
            saleTotal = "AUD1588.77";
        },
                    {
            saleTotal = "AUD1857.42";
        },
                    {
            saleTotal = "AUD1857.42";
        },
                    {
            saleTotal = "AUD1922.42";
        }
    );
};

}


- - - - 编辑。


使用这个模型。

class FlightDataModel {
    var carrier: String
    var price: String

    init(carrier: String?, price: String?) {
        self.carrier = carrier! 
        self.price = price!
    }
}

我将如何使用您的解决方案将其添加到 FlightDataModel 类的数组中

这是我的尝试..

var arrayOfFlights : [FlightDataModel] = [FlightDataModel]()



 if let tripOptions = trips["tripOption"] as? [[String:AnyObject]] {
                        for (index, tripOption) in enumerate(tripOptions) {
                            //println("\(index): " + (tripOption["saleTotal"]! as String))

                            self.arrayOfFlights[index].carrier = tripOption["saleTotal"]! as String


                            println("\(self.arrayOfFlights[index].carrier)")
                        }
4

1 回答 1

1

Alamofire 可以做到,但必须深入研究你的 JSON 结构。:)

像这样,使用 Alamofire 的responseJSON方法:

    Alamofire.request(.GET, YOUR_URL, parameters: nil, encoding: .URL).responseJSON(options: NSJSONReadingOptions.allZeros) { (request, response, json, error) -> Void in
        if let myJSON = json as? [String:AnyObject] {
            if let trips = myJSON["trips"] as? [String:AnyObject] {
                if let data = trips["data"] as? [String:AnyObject] {
                    if let carriers = data["carrier"] as? [[String:String]] {
                        for (index, carrierName) in enumerate(carriers) {
                            println("\(index): " + carrierName["name"]!)
                        }
                    }
                }
                if let tripOptions = trips["tripOption"] as? [[String:AnyObject]] {
                    for (index, tripOption) in enumerate(tripOptions) {
                        println("\(index): " + (tripOption["saleTotal"]! as! String))
                    }
                }
            }
        }
    }

输出:

0: Cathay Pacific Airways Ltd.
1: Emirates
...
0: AUD1537.22
1: AUD1588.77
...

使用 SwiftyJSON 确实更容易一些。为了多样性,responseString这次我们将使用 Alamofire 的方法:

    Alamofire.request(.GET, YOUR_URL, parameters: nil, encoding: .URL).responseString(encoding: NSUTF8StringEncoding, completionHandler: {(request: NSURLRequest, response: NSHTTPURLResponse?, responseBody: String?, error: NSError?) -> Void in

        if let dataFromString = responseBody!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
            let json = JSON(data: dataFromString)
            let carriers = json["trips"]["data"]["carrier"].array
            for (index, carrier) in enumerate(carriers!) {
                println("\(index):" + carrier["name"].string!)
            }
            let tripOption = json["trips"]["tripOption"].array
            for (index, option) in enumerate(tripOption!) {
                println("\(index):" + option["saleTotal"].string!)
            }
        }

    })

输出:

0: Cathay Pacific Airways Ltd.
1: Emirates
...
0: AUD1537.22
1: AUD1588.77
...

注意:我enumerate以如何在获取内容的同时获取内容的索引为例。

于 2015-05-08T13:29:13.657 回答