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类:
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到对象类,变的非常的简单~~
利用京东联盟API获取自定义推广链接 - 56,245 Views苹果开发证书的配置(在Xcode中操作) - 20,059 ViewsIOS离线教程下载与Dash的使用 - 14,404 ViewsIOS拦截重定向请求(302)的几种方式 - 13,442 Views京东联盟API的使用 - 11,797 Views用lipo合并模拟器Framework与真机Framework - 10,681 Views中国移动光猫管理员帐号 - 10,168 Views完整的京东联盟自定义推广链接生产程序 - 10,027 ViewsXcode模拟器手动下载与安装方法 - 8,986 ViewsSwift调用摄像头拍照或者录制视频 - 8,848 Views
Aliyun (3)
Android (5)
BeeAPI (1)
C# (8)
Chrome (3)
frp (1)
FTP (3)
VsFTPd (1)
Git代码 (11)
https (8)
Init (12)
IOS开发 (33)
Javascript (2)
LAMP (49)
Apache (11)
Lighttpd (1)
MySQL (14)
Nginx (17)
postgresql (1)
Linux基础 (6)
VI (1)
lua (1)
memcached (2)
Netbeans (2)
NFS (1)
nodejs (1)
NPM (2)
OpenAPI (4)
openresty (1)
OSS (2)
OSX系统 (19)
Pandorabox (4)
PHP (32)
python (10)
redis (7)
SVN (1)
Swift (16)
Telegram (3)
VPN (1)
VPS (7)
wordpress (4)
XCode (8)
XSL (7)
其他技术 (11)
各类程序 (7)
日志管理 (2)
Awstats (1)
Webalizer (1)
站点相关 (9)
系统优化 (1)
系统安全 (6)
Fail2Ban (1)
IPtables (1)
系统管理 (19)
OpenSSH (1)
VPSMate (2)
Webmin (2)
邮件系统 (4)
Dovecot (1)
Postfix (2)