json - Swift: Filter a POST http request answer -


on swift, querying server http post request, with:

let myurl = nsurl(string: "http://****.*****.***.***/****.php");  let request = nsmutableurlrequest(url:myurl!); request.httpmethod = "post" let session = nsurlsession.sharedsession()  var getdefaults = nsuserdefaults.standarduserdefaults(); var password = getdefaults.valueforkey("password") as! string; var id = getdefaults.valueforkey("login") as! string;  var err: nserror?  let poststring = "method=tasks.gettasksbyfolder" + "&identifiant=" + id + "&password=" + password + "&data={\"folder\":\"" + folder + "\"}" // filter = {"folder":"inbox"}  request.httpbody = poststring.datausingencoding(nsutf8stringencoding); let task = nsurlsession.sharedsession().datataskwithrequest(request){     data,response,error in      if error != nil{         println("error=\(error)")         return     }  println("**** response = \(response)")   let responsestring = nsstring(data: data, encoding: nsutf8stringencoding) println("**** response data = \(responsestring)")   var json = nsjsonserialization.jsonobjectwithdata(data, options: .mutablecontainers, error: &err) as? nsdictionary   } task.resume() 

it works , example of returns me:

**** response data = optional({"result":true,"data":"[{\"id\":\"b43bd766295220b23279899d025217d18e98374a\",\"container_id\":\"6658\",\"created_by\":\"76bbfe695318d471a541bc3333e58eea28acae54\",\"creation_time\":\"2015-05-26 15:20:32\",\"last_modified_by\":null,\"last_modified_time\":null,\"is_deleted\":\"0\",\"deleted_by\":null,\"deleted_time\":null,\"percent\":\"0\",\"completed\":null,\"due\":null, 

etc......

i'm trying filter json encoded answer, indeed, want few informations, not of them. example, want "container_id", "creation_time" , "last_modified_by" print them on uitableview. how supposed them? on post request, filter on answer ? i've searched while on net , haven't found anything, excerpt use of json parser, https://github.com/owensd/json-swift have 1 problem with.. haven't json datas written on code, it's obtained http post request.. consequence,

if let last_modified_by = json["last_modified_by"].string{     println("last_modified_by = '\(last_modified_by)'") } 

got error "ambiguous use of 'string'"

hope concise enough, can edit post if need more code or explanations.

regards,

fselva

your json string wrong, there's " shouldn't there before array delimiter:

{"result":true,"data":"[{\"id\":\"b43bd766295                       ^ 

also, " result , data should escaped, later in string. example:

{\"result\":true,\"data\":[{\"id\":\"b43bd766295220b23279899d025217d18e98374a\", ...

after that, you're go:

var err: nserror? let json = nsjsonserialization.jsonobjectwithdata(data!, options: nil, error: &err) as? [string:anyobject] // cast `json!["data"]` array of dictionaries if let datadic = json!["data"] as? [[string:anyobject]] {     if let firstid = datadic[0]["id"] as? string {         println(firstid) // "b43bd766295220b23279899d025217d18e98374a"     } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -