如何使用AFNetworking检索JSON多数据?

时间:2015-02-03 15:05:45

标签: ios swift afnetworking

我正在使用带有Swift的AFNetworking Library。 当我检索单个数据json如[{a:“xxx”,b:“yyy”}]时,它成功了。 但是当我检索多个数据json,如[{a:“xxx”,b:“yyy”},{a:“mmm”,b:“nnn”}]时,它失败了。

    let manager:AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()
    let serializer:AFJSONRequestSerializer = AFJSONRequestSerializer()
    manager.requestSerializer = serializer
    manager.GET("http://www.test.com/member.json", parameters: nil,
        success: {(operation: AFHTTPRequestOperation!, responsObject: AnyObject!) in
            let responsDict = responsObject as Dictionary<String, AnyObject>
        })

我想我应该将responsObject Dictionary的类型更改为其他内容。 但我不知道。

请你给我建议。

1 个答案:

答案 0 :(得分:1)

请查看以下示例,了解您缺少的内容:

manager.GET( "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22AAPL%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=",
    parameters: nil,
    success: {
        operation, responseObject in

        if let quote = responseObject?.objectForKey("query")?.objectForKey("results")?.objectForKey("quote") as? NSDictionary {

            let symbol = quote.objectForKey("Symbol") as? String
            let lastTradePriceOnly = quote.objectForKey("LastTradePriceOnly") as? String

            println("results: \(symbol) @ \(lastTradePriceOnly)")
        } else {
            println("no quote")
        }
    },
    failure: {
        operation, error in

        println("Error: " + error.localizedDescription)
    })