数据地址
let _urlPath = NSBundle.mainBundle().pathForResource("news_List.Json",ofType: nil)
1.对对象进行解析
//解析对象 func JsonUser(){ //Swift对象 let user = [ "user": "张三","@R_502_6889@": ["@R_502_6889@1": "大名","@R_502_6889@2": "小名"] ] //首先判断能不能转换 if (!NSJsONSerialization.isValIDJsONObject(user)) { print("is not a valID Json object") return } //将对象转换为NSData let data : NSData! = try? NSJsONSerialization.dataWithJsONObject(user,options: []) //把NSData对象转换回JsON对象 let Json : AnyObject! = try? NSJsONSerialization .JsONObjectWithData(data,options:NSJsONReadingOptions.AllowFragments) //验证JsON对象可用性 let users : AnyObject = Json.objectForKey("user")! let @R_502_6889@1 : AnyObject = Json.objectForKey("@R_502_6889@")!.objectForKey("@R_502_6889@1")! let @R_502_6889@2 : AnyObject = Json.objectForKey("@R_502_6889@")!.objectForKey("@R_502_6889@2")! print("users: \(users),@R_502_6889@1: \(@R_502_6889@1),@R_502_6889@2 :\(@R_502_6889@2)") }
2、对本地Json进行解析
func localData(){ let recvData2 = NSData.dataWithContentsOfMappedfile(_urlPath!) as! NSData let tempArray = try? NSJsONSerialization.JsONObjectWithData(recvData2,options: .MutableContainers) //Json解析然后强转 if (tempArray?.count > 0){ for (var i = 0; i < tempArray?.count; i++){ let oneVIDeo = tempArray?[i] as! NSDictionary //把里面转成字典 let ID = oneVIDeo.objectForKey("ID") let type = oneVIDeo.objectForKey("type") let Title = oneVIDeo.objectForKey("Title") let summary = oneVIDeo.objectForKey("summary") let image = oneVIDeo.objectForKey("image") let model = ZWModel() model.ID = ID as! Int model.type = type as! Int model.Title = Title as? String model.summary = summary as? String model.image = image as? String //把数据添加到模型数据 dataArray.addobject(model) } _tableVIEw?.reloadData() } }
3、使用NSURLConnection
func downloadData(){ //创建NSURL //请求本地数据 let url = NSURL(fileURLWithPath: _urlPath!) //请求网络数据 //let url = NSURL(string: _urlPath!) //以url创建请求 let request = NSURLRequest(URL:url!) //创建连接 _connection = NSURLConnection (request: request,delegate: self) }//收到http相应头 func connection(connection: NSURLConnection,dIDReceiveResponse response: NSURLResponse){ } //接收一段数据,这个函数会被调用多次 func connection(connection: NSURLConnection,dIDReceiveData data: NSData){ //把接收到的数据追加到_recvData后面 _recvData.appendData(data) } //数据接收完成 func connectionDIDFinishLoading(connection: NSURLConnection){ //接收完成函数,显示要解析的Json let str = Nsstring(data: _recvData,enCoding: NSUTF8StringEnCoding) //把二进制转化成字符串 print("s is \(str)") // let vIDeoArr = try? NSJsONSerialization.JsONObjectWithData(_recvData,options:.MutableContainers) //Json解析然后强转 if (vIDeoArr?.count > 0){ for (var i = 0; i < vIDeoArr?.count; i++){ let oneVIDeo = vIDeoArr?[i] as! NSDictionary //把里面转成字典 let ID = oneVIDeo.objectForKey("ID") let type = oneVIDeo.objectForKey("type") let Title = oneVIDeo.objectForKey("Title") let summary = oneVIDeo.objectForKey("summary") let image = oneVIDeo.objectForKey("image") let model = ZWModel() model.ID = ID as! Int model.type = type as! Int model.Title = Title as? String model.summary = summary as? String model.image = image as? String //把数据添加到模型数据 dataArray.addobject(model) } } //重新刷新表格 _tableVIEw?.reloadData() }
4、网络数据的请求和解析将(3)中的//请求网络数据 //let url = NSURL(string: _urlPath!)
放开,地址改为网络地址
以上是内存溢出为你收集整理的Swift-网络、本地数据解析全部内容,希望文章能够帮你解决Swift-网络、本地数据解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)