Get value from JSON file in Swift -
i have json file converted nsdictionary object. question how 1 single value out object? make httppost webside , json array values "success" , "userid" want check on success if true or false.
import uikit class viewcontroller2: uiviewcontroller { override func viewdidload() { super.viewdidload() // additional setup after loading view. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func httpbtn(sender: anyobject) { posttoserver() httppost() } func posttoserver() { println("button presed") } func httppost() { var poststring = "email=joakim@and.dk&password=123456" //declare url var url: nsurl! = nsurl(string: "http://95.85.53.176/nhi/api/app/login") var request: nsmutableurlrequest = nsmutableurlrequest(url: url) //declare httpmethod request.httpmethod = "post" //post data 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)") var responsestring = nsstring(data: data, encoding: nsutf8stringencoding) println("responsestring =\(responsestring)") var error: nserror? var myjson = nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves, error: &error) as? nsdictionary println(myjson) } task.resume() } }
output
responsestring =optional({"success":true,"userid":"62"}) optional({ success = 1; userid = 62; })
you can access using key value like
var success = myjson["success"]!.intvalue var userid = myjson["userid"]!.intvalue if(success == 1) //if true { //do }
same userid
Comments
Post a Comment