【问题标题】:Swift - How to use ObjectMapper to read Flickr JSONSwift - 如何使用 ObjectMapper 读取 Flickr JSON
【发布时间】:2017-06-15 04:56:20
【问题描述】:

我想使用 URL 使用来自 Flickr 的公开供稿中的 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)!

返回:

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"]
    }

在查看照片公共 URL 时,我注意到响应将照片对象整理到一个名为 "items": 的数组中

我读到我可以使用AlamofireObjectMapper 来提供帮助;但我对如何实际使用它感到困惑。

无论如何,我的查询具体是如何使用 ObjectMapper 在 Flickr Public 照片 Feed 中使用 items 数组?

非常感谢

【问题讨论】:

    标签: json alamofire flickr objectmapper


    【解决方案1】:

    我能够使用 AlamofireObjectMapper 解决这个问题

    首先,创建一个类来保存返回的响应。 其次确保这个类符合映射;

    // 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 时,我稍微更改它以使其符合此类;

    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&lt;PhotoResponse&gt;) in 行。

    最后,当我们通过 fetchPublicPhotos 函数调用它时,我可以查询返回的结果

    let photoResponse = response?.result.value
    
    if let items = photoResponse?.items {
        print ("Items found: #\(items.count)")
       // do something with items here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      相关资源
      最近更新 更多