swift的AFNetworking请求数据和json解析

这篇博客介绍了如何在Swift中利用AFNetworking进行网络请求,获取JSON数据,并进行解析。通过创建一个ViewController,实现UITableView的数据源和代理方法,展示从服务器获取的商品列表。示例代码中详细展示了请求参数设置、AFHTTPRequestOperationManager的使用、JSON解析以及将数据填充到表格视图的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  ViewController.swift

//  AFNetworking_swiftDemo

//

//  Created by apple on 15/7/27.

//  Copyright (c) 2015 紫晶立方科技有限公司. All rights reserved.

//


import UIKit


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{

    var tableView:UITableView?

    var array = [AnyObject]()

    override func viewDidLoad() {

        super.viewDidLoad()

        self.createTableView()

        self.loadData()


        

    }

    func createTableView()

    {

        self.tableView = UITableView(frame: CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-20), style: UITableViewStyle.Plain)

        self.tableView?.delegate = self

        self.tableView?.dataSource = self

        //self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "swiftCell")

        self.tableView?.registerNib(UINib(nibName: "goodDetailCell", bundle: nil), forCellReuseIdentifier: "swiftCell")

        self.view.addSubview(self.tableView!)

    }

    func loadData ()

    {

        

        var paramDict: Dictionary<String, String> = ["pagesize":"20", "pagenumber":"1"]

        var afManager = AFHTTPRequestOperationManager()

        var op = afManager.GET("http://www.3dshijian.com/interface/listgoods", parameters: paramDict, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in

            

            var jsonResult: AnyObject! = NSJSONSerialization.JSONObjectWithData(responseObject as! NSData, options: NSJSONReadingOptions.AllowFragments, error: nil)

            //获取json里的数据放入数组

            if let jsonDic = jsonResult as? NSDictionary {

                if let dataDic = jsonDic["data"] as? NSDictionary {

                    if let listArray = dataDic["list"] as? NSArray {

                        

                        for value in listArray {

                            

                            self.array.append(value)

                            var model = goodModel()

                            

                        }

                        self.tableView?.reloadData()

                                            }

                }

                

            }

            

            }) { (operation: AFHTTPRequestOperation!,

                error: NSError!) -> Void in

            

        }

        op.responseSerializer = AFHTTPResponseSerializer()

        op.start()

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let identify: String = "swiftCell"

        let cell:goodDetailCell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as! goodDetailCell

        

        if let detailDic = self.array[indexPath.row] as? NSDictionary {

            

            cell.loadData(detailDic)

        }

        return cell

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        

        return self.array.count

    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 100

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        println(indexPath.row)

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值