ios - 如何使用 AlamoFire/ObjectMapper 访问嵌入在 JSON 字典中的链接?

标签 ios json swift alamofire objectmapper

这是我正在使用的对象映射器:

ObjectMapper/AlamoFireObjctMapper

这是我正在使用的 JSON 数据,我正在尝试访问“链接”值:

channels =     (
  {
    social =             {
      facebook =                 {
        "facebook_id" = 47360808996;
        link = "https://www.facebook.com/CBS";
      };
      twitter =                 {
        link = "https://twitter.com/CBS";
        "twitter_id" = 97739866;
      }
    }
  }
)

我创建了自定义对象来表示 JSON 字典的每个级别:

class SocialInfo: Mappable {

  var channels: Social?

  required init?(map: Map) {
  }

  func mapping(map: Map) {

    channels <- map["channels"]

  }
}

class Social: Mappable {

  var facebookSocial: Facebook?
  var twitterSocial: Twitter?


  required init?(map: Map) {
  }

  func mapping(map: Map) {

     facebookSocial <- map["facebook"]
     twitterSocial <- map["twitter"]

  }

}


class Facebook: Mappable {

  var facebookLink: NSURL?

  required init?(map: Map) {
  }

  func mapping(map: Map) {

    facebookLink <- (map["link"], URLTransform())

  }
}

最初,我将“Facebook”类下的“facebookLink”作为字符串,但它一直返回零。然后我尝试使用 URLTransform 将其更改为 NSURL 类型,但现在它抛出错误:

Type of expression is ambiguous without more context

以下是我尝试检索 JSON 数据的方法:

 func getSocialInfo (completionHandler: @escaping (SocialInfo?, Error?) -> ()){

    Alamofire.request("\(baseURL)/\(apiKey)/show/950", method: .get, encoding: JSONEncoding.default).validate ()
      .responseObject{  (response: DataResponse<SocialInfo>) in

        switch response.result {
        case .success:
          let socialInfo = response.result.value

          print("This is the social info: \(socialInfo?.channels?.facebookSocial?.facebookLink)")


          completionHandler(socialInfo!, nil)

        case .failure(let error):

          print("Sorry there was an error: \(error)")
          completionHandler(nil,error)
          return
        }

  }
}

最佳答案

尝试将链接设置为字符串

var stringURL:String?

获取链接

stringURL <- map["link"]

stringURL转换为URL

if let url = URL(string: stringURL) {
// do something 
}

另请注意,这不会起作用,因为您特别指出 facebookDictionary is = 到您的 Facebook 对象。

 class Social: Mappable {

  var facebookSocial: Facebook?
  var twitterSocial: Twitter?

  fileprivate var facebookDictionary:[AnyHashable:Any]? 
  fileprivate var twitterDictionary:[AnyHashable:Any]?
  required init?(map: Map) {
  }

  func mapping(map: Map) {

     facebookDictionary <- map["facebook"]
     twitterDictionary <- map["twitter"]

     if let _facebookDictionary = facebookDictionary,
        let _twitterDictionary = wittertDictionary {

        let twitterMap = Map(mappingType: .fromJSON, JSON: _twitterDictionary)
        let facebookMap = Map(mappingType: .fromJSON, JSON: _facebookDictionary)

     /// Here you can initiate both object using map.

        facebookSocial = Facebook(map: facebookMap)
        twitterSocial = Twitter(map: twitterMap)

    /// If you want to use mapping function, set this as:

        facebookSocial = Facebook()
        twitterSocial.mapping(map: twitterMap)

        facebookSocial = Facebook()
        twitterSocial.mapping(map: twitterMap)
     }

  }

}

由于您没有使用社交词典创建新 map ,因此这两个对象将始终nil

社交相同,首先从社交字典创建一个 map

关于ios - 如何使用 AlamoFire/ObjectMapper 访问嵌入在 JSON 字典中的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109526/

相关文章:

ios - Swift - AVAudioPlayer 不播放声音

iphone - 当应用程序在后台或设备被锁定时运行代码

javascript - 使用 javascript 将检索到的数据保存到本地 json 数据库中

javascript - 发出 JSON RPC API 的 GET 请求而不是 POST 请求

swift - UImageView 的动画位置

swift - 使用或扩展类更好吗? ( swift )

ios - 隐藏在 View 后面的 UIAlertView

iphone - 当我们在 iOS 中使用 delegate 和 Call back 时?

javascript - 如何检测输入文本字段中的更改并启用保存按钮

ios - 更改图像属性 UITableView Swift