首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Json Yahoo Api错误

Json Yahoo Api错误
EN

Stack Overflow用户
提问于 2016-03-01 02:19:04
回答 1查看 322关注 0票数 0

我一直在开发一个应用程序,该应用程序使用雅虎金融公司()的jSon获取股票数据,但我的错误代码出现了问题。它在the语句中表示未解析的标识符jsonDict和预期的表达式错误。

这是密码。

代码语言:javascript
复制
import Foundation

let kNotificationStocksUpdated = "stocksUpdated"

class StockManagerSingleton {

//Singleton Init
class var sharedInstance : StockManagerSingleton {
    struct Static {
        static let instance : StockManagerSingleton = StockManagerSingleton()
    }
    return Static.instance
}

/*!
* @discussion Function that given an array of symbols, get their stock prizes from yahoo and send them inside a NSNotification UserInfo
* @param stocks An Array of tuples with the symbols in position 0 of each tuple
*/
func updateListOfSymbols(stocks:Array<(String,Double)>) ->() {

    //1: YAHOO Finance API: Request for a list of symbols example:
    //http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN ("AAPL","GOOG","FB")&format=json&env=http://datatables.org/alltables.env

    //2: Build the URL as above with our array of symbols
    var stringQuotes = "(";
    for quoteTuple in stocks {
        stringQuotes = stringQuotes+"\""+quoteTuple.0+"\","
    }
    stringQuotes = stringQuotes.substringToIndex(stringQuotes.endIndex.predecessor())
    stringQuotes = stringQuotes + ")"

    let urlString:String = ("http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN "+stringQuotes+"&format=json&env=http://datatables.org/alltables.env").stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
    let url : NSURL = NSURL(string: urlString)!

    let request: NSURLRequest = NSURLRequest(URL:url)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    //3: Completion block/Clousure for the NSURLSessionDataTask
    let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

        do {
            if let _ = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary {
                // Success block...
            }
        } catch {
            print(error)
        }

        else {
                //5: Extract the Quotes and Values and send them inside a NSNotification
                var quotes:NSArray = ((jsonDict.objectForKey("query") as NSDictionary).objectForKey("results") as NSDictionary).objectForKey("quote") as NSArray
                dispatch_async(dispatch_get_main_queue(), {
                    NSNotificationCenter.defaultCenter().postNotificationName(kNotificationStocksUpdated, object: nil, userInfo: [kNotificationStocksUpdated:quotes])
                })
            }
    })


    //6: DONT FORGET to LAUNCH the NSURLSessionDataTask!!!!!!
    task.resume()
}
}

有问题的代码行是。

代码语言:javascript
复制
var quotes:NSArray = ((jsonDict.objectForKey("query") as NSDictionary).objectForKey("results") as NSDictionary).objectForKey("quote") as NSArray
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-01 06:02:14

目前,您有一些没有意义的代码。例如,else没有对应的if。我不知道您想用您的代码做什么,但是下面的代码看起来应该是这样的。请注意,这也是错误的。您需要回到原始代码的来源,因为您当前的代码将永远无法工作。

代码语言:javascript
复制
func updateListOfSymbols(stocks:Array<(String,Double)>) ->() {

    //1: YAHOO Finance API: Request for a list of symbols example:
    //http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN ("AAPL","GOOG","FB")&format=json&env=http://datatables.org/alltables.env

    //2: Build the URL as above with our array of symbols
    var stringQuotes = "(";
    for quoteTuple in stocks {
        stringQuotes = stringQuotes+"\""+quoteTuple.0+"\","
    }
    stringQuotes = stringQuotes.substringToIndex(stringQuotes.endIndex.predecessor())
    stringQuotes = stringQuotes + ")"

    let urlString:String = ("http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN "+stringQuotes+"&format=json&env=http://datatables.org/alltables.env").stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
    let url : NSURL = NSURL(string: urlString)!

    let request: NSURLRequest = NSURLRequest(URL:url)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    //3: Completion block/Clousure for the NSURLSessionDataTask
    let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

        do {
            if let jsonDict = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary {
                // Success block...
                //5: Extract the Quotes and Values and send them inside a NSNotification
                var quotes:NSArray = ((jsonDict.objectForKey("query") as NSDictionary).objectForKey("results") as NSDictionary).objectForKey("quote") as NSArray
                dispatch_async(dispatch_get_main_queue(), {
                    NSNotificationCenter.defaultCenter().postNotificationName(kNotificationStocksUpdated, object: nil, userInfo: [kNotificationStocksUpdated:quotes])
                })
            }
        } catch {
            print(error)
        }

    })


    //6: DONT FORGET to LAUNCH the NSURLSessionDataTask!!!!!!
    task.resume()
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35713531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档