javascript - Parsing a json response from swift -
i have following code manage response via json form api.
let url: nsurl = nsurl(string: "http://jsonplaceholder.typicode.com/posts")! let request:nsmutableurlrequest = nsmutableurlrequest(url:url) request.httpmethod = "post" let bodydata = "title=foo&body=bar&userid=1" request.httpbody = bodydata.datausingencoding(nsutf8stringencoding); nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.mainqueue()){ (response, data, error) in let res = nsstring(data: data!, encoding: nsutf8stringencoding) print(res) }
i trying learn swift thought bit javascript getting response this.
optional({ "title": "foo", "body": "bar", "userid": 1, "id": 101 })
normally javascript value running response.body or along lines cannot figure out how parse json response swift.
any please
thanks
you need convert json response swift object so:
let res = nsjsonserialization.jsonobjectwithdata(data!, options: .allowfragments) as! [string:anyobject]
then can access values within like:
let title = res["title"]
Comments
Post a Comment