IOS网络操作-使用Alamofire和ObjectMapper处理JSON转对象

Alamofire是一个使用Swift语言写的一个网络库,操作很简单,结合ObjectMapper、AlamofireObjectMapper,可以轻松的将JSON数据转换为对象类型!

Alamofire:https://github.com/Alamofire/Alamofire

ObjectMapper:https://github.com/Hearst-DD/ObjectMapper

ALamoObjectmapper:https://github.com/tristanhimmelman/AlamofireObjectMapper

假设有个地址返回如下JSON数据:

{
  "meta": {
    "total": "16868",
    "time": "0.435",
    "limit": "10",
    "offset": 0,
    "currency": "CNY",
    "symbol": "¥"
  },
  "products": [
    {
      "time_updated": "1432808931",
      "commission": "363.42",
      "price": "6,056.92",
      "sku": "517569",
      "upc": "",
      "pweight": null,
      "product_id": "82808bcd27eeb65dd15740aee40ec07c",
      "product_image": "http://images.rewardstyle.com/img?v=1.3&p=82808bcd27eeb65dd15740aee40ec07c",
      "product_type": "1",
      "product_name": "Cape-back crepe and chiffon mini dress",
      "advertiser": "NET-A-PORTER UK",
      "designer": "Gareth Pugh",
      "category": "WOMEN->Clothing->Dresses"
    },
    {
      "time_updated": "1432808931",
      "commission": "1,359.23",
      "price": "22,653.85",
      "sku": "549499",
      "upc": "",
      "pweight": null,
      "product_id": "e147fbbf30c65538b367f3a6ab539252",
      "product_image": "http://images.rewardstyle.com/img?v=1.3&p=e147fbbf30c65538b367f3a6ab539252",
      "product_type": "1",
      "product_name": "Woven suede tote",
      "advertiser": "NET-A-PORTER UK",
      "designer": "Loewe",
      "category": "WOMEN->Bags->Tote"
    }]
}
使用对象的概念来解读这段JSON数据,它包含Meta和Product:Meta是一个对象,Products是一个数组类型,包含了多个Product对象。

我们首先使用ObjectMapper库定义的规则,来写一个实体类。

class Meta : Mappable{
    var total:String?
    var time:Double?
    var limit:String?
    var offset:String?
    var currency:String?
    var symbol:String?
    init(){}
    required init?(_ map: Map){
        mapping(map)
    }
    func mapping(map: Map) {
        total <- map["total"]
        time <- map["time"]
        limit <- map["limit"]
        offset <- map["offset"]
        currency <- map["currency"]
        symbol <- map["symbol"]
    }
}


class Product : Mappable{

    var time_updated:String?
    var commission:String?
    var price:String?
    var sku:String?
    var product_id:String?
    var product_image:String?
    var product_name:String?
    var advertiser:String?
    var designer:String?
    var category:String?
    init(){}
    
    required init?(_ map: Map) {
        mapping(map)
    }
    
    func mapping(map: Map) {
        time_updated <- map["prtime_updated"]
        commission <- map["age"]
        price <- map["price"]
        sku <- map["sku"]
        product_id <- map["product_id"]
        product_image <- map["product_image"]
        product_name <- map["product_name"]
        advertiser <- map["advertiser"]
        designer <- map["designer"]
        category <- map["category"]
    }
}
每个实体类都实现了Mappable协议。这仅仅是JSON数据中的两个对象,现在我们要再声明下整个JSON数据的对象定义,它将作为顶层包含着Meta类和Product类:
class ProductResponse:Mappable{
    var meta:Meta?
    var products:[Product]?
    
    init(){}
    required init?(_ map: Map) {
        mapping(map)
    }
    func mapping(map: Map) {
        meta <- map["meta"]
        products <- map["products"]
    }
}
接下来,就可以使用Alamofire来操作了。这时候,可以使用AlamofireObjectMapper库,它使用extension扩展了Alamofire的response方法:
Alamofire.request(Method.GET, "请求的地址", parameters: nil, encoding: ParameterEncoding.URL).responseObject { (response:ProductResponse?, error:NSError?) -> Void in
    if let products = response?.products{
        for p in products{
            println(p.product_name)
        }
    }
}// end request
json到对象类,变的非常的简单~~

tips:

本文由wp2osc导入,原文链接:http://devonios.com/ios-network-alamofire.html

由于OSChina的OpenAPI在处理content参数时会自动过滤img标签,所以无法显示图片,详见

转载于:https://my.oschina.net/lijialong/blog/474363

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值