let urlPath: String = "http://binaenaleyh.net/dusor/"
var url: NSURL = NSURL(string: urlPath)
var imgData: NSData = NSData(contentsOfURL: url)
let jsonDict = NSJSONSerialization.JSONObjectWithData(imgData, options: nil, error: &error) as NSDictionary
println(jsonDict["dersler"])
It returns the whole JSON value, but I need the value at a specific index only.
I take it jsonDict
is either an NSDictionary
or a [NSObject:AnyObject]
? First off, you shouldn't be using valueForKey()
. The appropriate method on NSDictionary
is objectForKey
. But even better, you can just subscript it.
Of course, the results of the subscript are a value typed as AnyObject
, and you can't just subscript that. You need to cast it to the appropriate type. If you know the cast will succeed you can just use as
, otherwise you'll need as?
. Assuming the latter, this would look like
if let ary = jsonDict["dersler"] as? [Int] {
let x: Int = ary[0]
println(x)
}
Surely you can just do...
println(jsonDict["dersler"][0])
?
This has been asked many times too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.