Reputation: 6049
Solved:
I was able to solve the problem in question with this:
if let returnedData = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? NSString? {
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
let trimmedDataString = ((dataString.stringByReplacingOccurrencesOfString("YAHOO.Finance.SymbolSuggest.ssCallback", withString: "") as NSString).stringByReplacingOccurrencesOfString("(", withString: "") as NSString).stringByReplacingOccurrencesOfString(")", withString: "")
let dataFromDataString = trimmedDataString.dataUsingEncoding(NSUTF8StringEncoding)!
if let jsonObject: AnyObject = NSJSONSerialization.JSONObjectWithData(dataFromDataString, options: .allZeros, error: nil) {
if let resultSet = jsonObject["ResultSet"] as! NSDictionary? {
println(resultSet)
}
}
}
Original Question:
I am currently using Yahoo! Finance's YQL to return data for stocks. I am able to query stocks and successfully return the wanted information, but only if I know the stock's symbol (AAPL, GOOG, FDX). However, I would like to be able to replicate the search functionality of iOS' Stocks application, which lets you perform a live search of stock symbols as you type a string.
When using this URL to retrieve the information, I get a JSON response that is of no use to me:
{"query":{"count":0,"created":"2015-06-01T23:49:49Z","lang":"en-us","results":null}}
However, I have been able to find what I am looking for from this URL, but the data is not being returned in JSON format that I currently know how to handle:
YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"k","Result":[{"symbol":"K","name":"Kellogg Company","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"KNDI","name":"Kandi Technologies Group, Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"KO","name":"The Coca-Cola Company","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"KMI","name":"Kinder Morgan, Inc.","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"KMB","name":"Kimberly-Clark Corporation","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"GMCR","name":"Keurig Green Mountain, Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"KORS","name":"Michael Kors Holdings Limited","exch":"NYQ","type":"S","exchDisp":"NYSE","typeDisp":"Equity"},{"symbol":"KLAC","name":"KLA-Tencor Corporation","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"KRFT","name":"Kraft Foods Group, Inc.","exch":"NMS","type":"S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"^KLSE","name":"FTSE Bursa Malaysia KLCI","exch":"KLS","type":"I","exchDisp":"Kuala Lumpur Stock Exchange ","typeDisp":"Index"}]}})
I have been searching all day for a way to convert this returned string into a JSON object for the use that I need in my application.
Since it is not coming back in a JSON format that I know how to parse with NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil)
, I have been stuck in search for a way to create a JSON object from the string. Is there a direction that I can be pointed in that I'm just missing?
Upvotes: 0
Views: 293
Reputation: 14845
Try this:
if let returnedData = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? NSString? {
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
if let jsonObject: AnyObject = NSJSONSerialization.JSONObjectWithData(dataString, options: .allZeros, error: nil) {
if let resultSet = resultSet[yourDictionaryObject objectForKey:@"YAHOO.Finance.SymbolSuggest.ssCallback"]
//This should return a array of dictionary
}
}
Upvotes: 1