Swift - 如何使用ObjectMapper阅读Flickr JSON
Swift - How to use ObjectMapper to read Flickr JSON
我想使用 URL
从 Flickr public 可用的提要中消费 JSON
https://api.flickr.com/services/feeds/photos_public.gne?nojsoncallback=1&format=json
我正在尝试使用 Alamofire 消耗 JSON,然后使用 ObjectMapper 将 JSON 映射到基本照片数据模型。
我遇到的问题是我无法让它解析 JSON 然后映射它。
objects = Mapper<Photo>().mapArray(JSONArray: json)!
Returns:
Cannot convert value of type 'JSON' to expected argument type '[[String : Any]]'
我的代码如下;
FlickrAPI.executeRequestURL(url, parameters: parameters) { (success, error, response) in
if (success) {
//print ("Response: \(response)")
if let jsonObject = response?.result.value {
let json = JSON(jsonObject)
objects = Mapper<Photo>().mapArray(JSONArray: json)!
}
}
else {
print ("** Error -- \(error?.localizedDescription) **")
taskCallback(false, error)
}
}
// Execute URL request code:
static func executeRequestURL(_ requestURL: URL, parameters:[String: String], taskCallback: @escaping (Bool, Error?, DataResponse<Any>?) -> ())
{
print ("Attempting URL -- \(requestURL)")
Alamofire.request(requestURL, parameters:["nojsoncallback": "1", "format": "json"])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch(response.result) {
case .success(_):
taskCallback(true, nil, response)
break
case .failure(_):
print("Error while fetching url: \(response.result.error)")
taskCallback(false, response.result.error, nil)
break
}
}
}
// Photo model
final class Photo: Object, Mappable {
private(set) dynamic var uuid: String = UUID().uuidString
dynamic var name: String = ""
dynamic var author_id: String = ""
dynamic var title: String = ""
override public static func primaryKey() -> String? {
return "uuid"
}
required convenience public init?(map: Map) {
self.init()
}
// MARK: - ObjectMapper protocol methods
public func mapping(map: Map) {
self.name <- map["name"]
self.author_id <- map["author_id"]
self.title <- map["title"]
}
查看照片时 public URL 我注意到响应将照片对象整理在一个名为 "items":
的数组中
我读到我可以使用 AlamofireObjectMapper
来帮助;但我对如何实际使用它感到困惑。
无论如何,我的问题是如何使用 ObjectMapper 在 Flickr Public 照片提要中使用 items
数组?
非常感谢
我能够使用 AlamofireObjectMapper 解决这个问题
首先,创建一个 class 来保存返回的响应。
其次确保这个 class 符合映射;
// Required to help integrate Alamofire with Objectmapper (via AlamofireObjectMapper)
final class PhotoResponse: Mappable {
var items: [Photo]?
required init?(map: Map){
}
func mapping(map: Map) {
items <- map["items"]
}
}
然后当我调用执行 URL 时,我稍微更改它以使其符合此 class;
static func executeRequestURL(_ requestURL: URL, parameters:[String: String], taskCallback: @escaping (Bool, Error?, DataResponse<PhotoResponse>?) -> ())
{
print ("Attempting URL -- \(requestURL)\n")
Alamofire.request(requestURL, parameters:["nojsoncallback": "1", "format": "json"])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseObject { (response: DataResponse<PhotoResponse>) in
switch(response.result) {
case .success(_):
taskCallback(true, nil, response)
break
case .failure(_):
print("** Error while fetching url: \(response.result.error) **")
taskCallback(false, response.result.error, nil)
break
}
}
}
这里的关键部分是 response: DataResponse<PhotoResponse>) in
行。
最后,当我们通过 fetchPublicPhotos
函数调用它时,我可以查询返回的结果
let photoResponse = response?.result.value
if let items = photoResponse?.items {
print ("Items found: #\(items.count)")
// do something with items here
}
我想使用 URL
从 Flickr public 可用的提要中消费 JSONhttps://api.flickr.com/services/feeds/photos_public.gne?nojsoncallback=1&format=json
我正在尝试使用 Alamofire 消耗 JSON,然后使用 ObjectMapper 将 JSON 映射到基本照片数据模型。
我遇到的问题是我无法让它解析 JSON 然后映射它。
objects = Mapper<Photo>().mapArray(JSONArray: json)!
Returns:
Cannot convert value of type 'JSON' to expected argument type '[[String : Any]]'
我的代码如下;
FlickrAPI.executeRequestURL(url, parameters: parameters) { (success, error, response) in
if (success) {
//print ("Response: \(response)")
if let jsonObject = response?.result.value {
let json = JSON(jsonObject)
objects = Mapper<Photo>().mapArray(JSONArray: json)!
}
}
else {
print ("** Error -- \(error?.localizedDescription) **")
taskCallback(false, error)
}
}
// Execute URL request code:
static func executeRequestURL(_ requestURL: URL, parameters:[String: String], taskCallback: @escaping (Bool, Error?, DataResponse<Any>?) -> ())
{
print ("Attempting URL -- \(requestURL)")
Alamofire.request(requestURL, parameters:["nojsoncallback": "1", "format": "json"])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch(response.result) {
case .success(_):
taskCallback(true, nil, response)
break
case .failure(_):
print("Error while fetching url: \(response.result.error)")
taskCallback(false, response.result.error, nil)
break
}
}
}
// Photo model
final class Photo: Object, Mappable {
private(set) dynamic var uuid: String = UUID().uuidString
dynamic var name: String = ""
dynamic var author_id: String = ""
dynamic var title: String = ""
override public static func primaryKey() -> String? {
return "uuid"
}
required convenience public init?(map: Map) {
self.init()
}
// MARK: - ObjectMapper protocol methods
public func mapping(map: Map) {
self.name <- map["name"]
self.author_id <- map["author_id"]
self.title <- map["title"]
}
查看照片时 public URL 我注意到响应将照片对象整理在一个名为 "items":
我读到我可以使用 AlamofireObjectMapper
来帮助;但我对如何实际使用它感到困惑。
无论如何,我的问题是如何使用 ObjectMapper 在 Flickr Public 照片提要中使用 items
数组?
非常感谢
我能够使用 AlamofireObjectMapper 解决这个问题
首先,创建一个 class 来保存返回的响应。 其次确保这个 class 符合映射;
// Required to help integrate Alamofire with Objectmapper (via AlamofireObjectMapper)
final class PhotoResponse: Mappable {
var items: [Photo]?
required init?(map: Map){
}
func mapping(map: Map) {
items <- map["items"]
}
}
然后当我调用执行 URL 时,我稍微更改它以使其符合此 class;
static func executeRequestURL(_ requestURL: URL, parameters:[String: String], taskCallback: @escaping (Bool, Error?, DataResponse<PhotoResponse>?) -> ())
{
print ("Attempting URL -- \(requestURL)\n")
Alamofire.request(requestURL, parameters:["nojsoncallback": "1", "format": "json"])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseObject { (response: DataResponse<PhotoResponse>) in
switch(response.result) {
case .success(_):
taskCallback(true, nil, response)
break
case .failure(_):
print("** Error while fetching url: \(response.result.error) **")
taskCallback(false, response.result.error, nil)
break
}
}
}
这里的关键部分是 response: DataResponse<PhotoResponse>) in
行。
最后,当我们通过 fetchPublicPhotos
函数调用它时,我可以查询返回的结果
let photoResponse = response?.result.value
if let items = photoResponse?.items {
print ("Items found: #\(items.count)")
// do something with items here
}