ios - How to parse data encode received from php in Swift? -
i receive data php file format:
{"object:value", "object2:value2"...} {"object:value", "object2:value2"...}
i know how parse in swift2 using next code repeatedly changing separatedby string:
if let url = nsurl(string: strurl), let data = nsdata(contentsofurl: url) { let strresult = nsstring(data: data, encoding: nsutf8stringencoding) } tareas = strresult!.componentsseparatedbystring(",")
but want parse more easily. have read others questions , answers format parse was:
[ {"person": {"name":"dani","age":"24"}}, {"person": {"name":"ray","age":"70"}} ]
and data different. how can more easily?
thanks!
you can read array
first of need convert data in valid json , access it
var result: nsarray = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) as! nsarray println("result: \(result)")
ex.
var str = "[{\"person\": {\"name\":\"dani\",\"age\":\"24\"}},{\"person\": {\"name\":\"ray\",\"age\":\"70\"}}]" var data : nsdata = str.datausingencoding(nsutf8stringencoding, allowlossyconversion: false)! var result: nsarray = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) as! nsarray println("response: \(result)") println("oth object: \(result[0])")
output
response: ( { person = { age = 24; name = dani; }; }, { person = { age = 70; name = ray; }; } ) oth object: { person = { age = 24; name = dani; }; }
Comments
Post a Comment