What should I do if a JSON file begins with an array? -
so trying use swifty-json go through json file. issue having though json file (from twitter api) starts array, example below:
[ { "created_at": "tue mar 31 13:47:02 +0000 2015", "id": 582901921171796000, } ] lets wanted read created_at part, following:
let urlpath = "http://api.twitter.com/1.1/statuses/home_timeline.json?authentication_info" let url: nsurl = nsurl(string: urlpath)! let session = nsurlsession.sharedsession() let task = session.datataskwithurl(url, completionhandler: {data, response, error -> void in if error != nil { // if there error in web request, print console println(error.localizeddescription) } var err: nserror? var jsonresult = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &err) nsdictionary if err != nil { // if there error parsing json, print console println("json error \(err!.localizeddescription)") } let json = json(jsonresult) let createdat = json[0]["created_at"].string! println(createdat) }) task.resume() this doesn't work though, retired error message highlights code , in console (lldb) printed out.
does know how in swifty-json, quit simple unsure.
thanks
cast response nsarray in nsjsonserialization:
var jsonresult = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &err) nsarray
Comments
Post a Comment