ios - swift unable to parse php data -
i have been trying fetch values database through poststring
, doesn't return value. php tested , working fine, appreciated. below code.
import uikit class displaydetailedviewcontroller: uiviewcontroller { var role = string() var date = string() var city = string() var mark_ref = string() override func viewdidload() { super.viewdidload() } override func didreceivememorywarning() { super.didreceivememorywarning() } func getdata(){ let myurl = url(string:"http://xxxxxx.co.in/xxxxx/show_report1.php") var request = urlrequest(url: myurl! url) request.httpmethod = "post" let poststring = "city=\(city)&date=\(date)&market_rep=\(mark_ref)" request.httpbody = poststring.data(using: string.encoding.utf8) let task = urlsession.shared.datatask(with: request) { (data: data?, response: urlresponse?, error: error?) in dispatchqueue.main.async{ if error != nil{ print("error is\(string(describing: error))") } do{ let json = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? nsdictionary if let parsejson = json { let myboard : string! myboard = parsejson["success"] as! string? print(myboard) } } catch { print(error) } } } task.resume() } }
try 1: replace below line (nsdictionary)
let json = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? nsdictionary
try 2: replace below line (nsarray)
let json = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? nsarray
try 3: replace below line (nsstring)
let json = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? nsarray
also try options: .mutablecontainers -> options: jsonserialization.readingoptions.allowfragments
Comments
Post a Comment