搞了两天解析返回的json数据终于出来了,下面就方向给大家
用到了第三的类库
SwiftHTTP (https://github.com/daltoniam/SwiftHTTP)做request get ,post ,put 等
SwiftJson(https://github.com/SwiftyJSON/SwiftyJSON) 讲返回的数据解析为json
关于第三方类库的引用网上文章很多 ,有不清楚的可以在评论里问我
下面说一下关键代码
//接受返回得来的数据转为json
var ggg = JSON(NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil)!)
println("fffffffffffffffffff")
println(ggg)
//这个是我接回来打印出来的json数据
{"msg":"success",
"dat":[
{"resid":"sc201255","fname":"姓名","lname":"PI","starttime":"3347","endtime":"3347","summary":"aaa","name":"S01","perm_modify":"1","perm_delete":"1","memberid":"485e138c89c"},
{"resid":"sc20150508161458","fname":"姓名","lname":"PIN","starttime":"3540","endtime":"3345","summary":"","name":"S01","perm_modify":"1","perm_delete":"1","memberid":"e138c89c"}]}
// 现在我要取出 dat中第一条里的 startime 如下
println(ggg["dat"][0]["starttime"])
//输出结果如下
3540
看起来是个比较简单的东西,不知道的话还是废了好久的时间,哈哈,接触swift 一个星期了终于能弄出点东西了,哈哈,有需要交流的给我留言啊,再提一句,github真心是个学习的好东东。
//我是放在一个工程的
viewDidLoad()里来实现的,其实就是为了最快的看到测试的结果,过几天把我的整个工程放到github上,到时再发布地址给大家吧
//一下是全部关键代码
overridefunc viewDidLoad() {
super.viewDidLoad()
var request = HTTPTask()
var params:Dictionary<String,AnyObject> = ["datetest":"14200","roomtest":"SH01","scheduleidtest":"sc15e5025f3d"]
//http://
request.GET("http://11.1.1.1..:8088/api/values/?sqlname=SELECT, parameters: params, success: {(response:HTTPResponse)in
iflet data = response.responseObjectas?NSData {
println("bbb\(count)")
let str =NSString(data: data, encoding:NSUTF8StringEncoding)
println("response:\(str)")//prints the HTML of the page
let jsonss =JSON(str!)
println(jsonss)
var jjj:AnyObject =NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments, error:nil)!
println(NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments, error: nil))
var ggg =JSON(NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments, error:nil)!)
println("fffffffffffffffffff")
println(ggg)
println(ggg["dat"][1]["starttime"])
println(ggg["dat"][0]["starttime"])
println(ggg.count)
}
},failure: {(error:NSError, response:HTTPResponse?) in
// println("error: \(error)")
})
}