我很沮丧,因为我想将JSON映射到Swift对象,我尝试了5个库,每次f ****** Swift都给我零。
因此,让我们通过提供以下方式让Instagram API提出简单的请求来获取用户数据:
user-id:237965875
client_id:b806368aaf384a7baec491f8fab610a6
我正在使用:
Podfile
平台:ios,'9.0'use_frameworks!
目标'secondTest'做pod'AlamofireObjectMapper','〜> 1.0'结束
和
XCODE 7
Swift 2.0
AlamofireObjectMapper v1.0
我知道我需要创建swift文件(类)并定义应该处理响应的对象:
答案是:(从apigee.com获得)HTTP/1.1 200 OK
Content-Language:
en
X-Ratelimit-Limit:
5000
Vary:
Cookie, Accept-Language, Accept-Encoding
Date:
Sun, 27 Sep 2015 18:46:45 GMT
Content-Length:
383
Expires:
Sat, 01 Jan 2000 00:00:00 GMT
X-Ratelimit-Remaining:
4987
Set-Cookie:
csrftoken=41681b429002fa50331def50b07b2a78; expires=Sun, 25-Sep-2016 18:46:45 GMT; Max-Age=31449600; Path=/
Connection:
keep-alive
Content-Type:
application/json; charset=utf-8
Server:
Apigee Router
Cache-Control:
private, no-cache, no-store, must-revalidate
Pragma:
no-cache
{
"meta":
{
"code": 200
},
"data":
{
"username": "jessica_brti",
"bio": "I write and make silly faces for a living.",
"website": "http://www.wattpad.com/JessicaBrti",
"profile_picture": "https://igcdn-photos-f-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/11311247_1206483099369077_1549111893_a.jpg",
"full_name": "Jessy",
"counts":
{
"media": 50,
"followed_by": 55153,
"follows": 46
},
"id": "237965875"
}
}
然后定义处理此响应的对象的swift类文件是:
import Foundation
import ObjectMapper
class instaResponse: Mappable
{
var metas: [meta]?
var datas: [data]?
required init?(_ map: Map){}
func mapping(map: Map) {
metas <- map["meta"]
datas <- map["data"]
}
}
class meta: Mappable
{
var code: String?
required init?(_ map: Map){}
func mapping(map: Map)
{
code <- map["code"]
}
}
class data: Mappable {
var username: String?
var bio: String?
var website: String?
var profile_picture: String?
var full_name: String?
var countss: [counts]?
var id: String?
required init?(_ map: Map){}
func mapping(map: Map)
{
username <- map["username"]
bio <- map["bio"]
website <- map["website"]
profile_picture <- map["profile_picture"]
full_name <- map["full_name"]
countss <- map["counts"]
id <- map["id"]
}
}
class counts: Mappable {
var media: Int?
var followed_by: Int?
var follows: Int?
required init?(_ map: Map){}
func mapping(map: Map)
{
media <- map["media"]
followed_by <- map["followed_by"]
follows <- map["follows"]
}
}
最后,这是我发出请求并将JSON解析为对象的文件
import UIKit
import Alamofire
import AlamofireObjectMapper
import ObjectMapper
class ViewController: UIViewController {
override func viewDidLoad() {
let userID =
[
"user-id":"237965875",
"client_id":"b806368aaf384a7baec491f8fab610a6"
]
let URL = "https://api.instagram.com/v1/users/237965875"
Alamofire.request(.GET, URL, parameters: userID)
.validate()
.responseObject
{
(result: instaResponse?, error: ErrorType?) in
print(error.debugDescription)
if let data = result?.datas
{
for i in data
{
print(i.username)
}
}
}.responseJSON{(_,_,result) in
print(result.value)
}
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最后,我正在使用打印的用户名
“print(i.username)”
只有一个人
零
和第二次印刷
“打印(result.value)”
正在给予闪亮的光芒:
Optional({
data = {
bio = "I write and make silly faces for a living.";
counts = {
"followed_by" = 55153;
follows = 46;
media = 50;
};
"full_name" = Jessy;
id = 237965875;
"profile_picture" = "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11311247_1206483099369077_1549111893_a.jpg";
username = "jessica_brti";
website = "http://www.wattpad.com/JessicaBrti";
};
meta = {
code = 200;
};
})
但我不知道如何使这个物体与Alamofire一致。
所以,伙计们,请帮助我解决这个难题。
提前致谢!!!
答案 0 :(得分:2)
您的问题是JSON中的meta
和data
元素只是对象,它们不是对象数组。因为您的映射将这些键映射到数组,所以无法对它们进行反序列化。
您的地图类应该是
import Foundation
import ObjectMapper
class instaResponse: Mappable
{
var metas: meta?
var datas: data?
required init?(_ map: Map){}
func mapping(map: Map) {
metas <- map["meta"]
datas <- map["data"]
}
}
...
然后你不需要for循环来访问数据 -
print(error.debugDescription)
if let data = result?.datas {
print(data.username)
}