【问题标题】:how to pass the API parameter and parameter is in array如何传递 API 参数和参数在数组中
【发布时间】:2019-05-08 03:46:25
【问题描述】:

如何传递数组参数

参数 [ { “身份证”:0, “追随者ID”:1030, “以下 ID”:1033, "followerName": "字符串", "followingName": "字符串", "createdDate": "字符串", “消息”:“字符串” } ]//how to solve this array

API函数

class func postFollowers(params:[String: Any],success:@escaping([FollowingDataProvider]) -> Void, failure:@escaping (String) -> Void){
    var request =  RequestObject()
    request = Services.servicePostForFollower(param: params)

    APIManager.Singleton.sharedInstance.callWebServiceWithRequest(rqst: request, withResponse: { (response) in
        if (response?.isValid)!
        {
            //success()
            print(response?.object as! JSON)
            success(self.followingJSONarser(responseObject: response?.object as! JSON));

            //followingJSONarser(responseObject: response?.object as! JSON)
        }
        else
        {
            failure((response?.error?.description)!)
        }
    }, withError: {
        (error) in
        failure((error?.description)!)
    })
}

解析

static func followingJSONarser(responseObject:JSON) -> [FollowingDataProvider]{
    var dataProvider = [FollowingDataProvider]()
    let jsonDataa = responseObject["data"]
    print(jsonDataa)
    let newJSON = jsonDataa["data"].arrayValue
    print(newJSON)


    for item in newJSON{
        print(item)
        dataProvider.append(FollowingDataProvider(id: item["userId"].intValue, followerId: item["userId"].intValue, followingId: item["followingId"].intValue, followerName: item["userName"].stringValue, followingName: item["followingName"].stringValue, createdDate: item["createdDate"].stringValue, message: item["message"].stringValue))
    }
    return dataProvider
}`

【问题讨论】:

    标签: swift api post alamofire


    【解决方案1】:

    您可以尝试将 SwiftyJsonCodable

    结合使用
    struct Root: Codable {
      let id, followerID, followingID: Int
      let followerName, followingName, createdDate, message: String
    
     enum CodingKeys: String, CodingKey {
        case id
        case followerID = "followerId"
        case followingID = "followingId"
        case followerName, followingName, createdDate, message
     }
    }
    

    if let con = response?.object as? JSON {
       do {
           let itemData = try con.rawData() 
           let res  = try JSONDecoder().decode([Root].self, from: itemData) 
           print(res)
       catch {
          print(error)
       }
    }
    

    同时避免使用 json 强制解包

    response?.object as! JSON
    

    【讨论】:

      【解决方案2】:

      按照您尝试从 API 解析数据的代码,您可以使用 SwiftyJSONAlamofire 创建 HTTP 请求(发布、获取、放置、删除等)

      您应该使用arrayObject 而不是arrayValue

      您的代码缺少正确的数据解析定义

      static func followingJSONarser(responseObject:JSON) -> [FollowingDataProvider]{
          var dataProvider = [FollowingDataProvider]()
          var itemClass = [ItemClass]()
          let jsonDataa = responseObject["data"] as! Dictionary
          let newJSON = jsonDataa["data"].arrayObject as! Array
      

      现在创建一个 dataModel 类来向它转换数据

      class ItemsClass:NSObject{
          var id:Int = 0
          var followerId:Int = 0
          var followingId:Int = 0
          var followerName:String = ""
          var followingName:String = ""
          var createdDate:String = ""
          var message:String = ""
      
          init(data:JSON) {
              self.id = data["userId"].intValue
              self.followerId = data["userId"].intValue
              self.followingId = data["followingId"].intValue
              self.followerName  = data["userName"].stringValue
              self.followingName = data["followingName"].stringValue
              self.createdDate = data["createdDate"].stringValue
              self.message = data["message"].stringValue
          }
      
      }
          for item in newJSON{
              dataProvider.append(itemClass)
          }
          return dataProvider
      }`
      

      【讨论】:

        猜你喜欢
        • 2010-09-12
        • 1970-01-01
        • 1970-01-01
        • 2018-07-05
        • 2023-03-24
        • 1970-01-01
        • 2016-08-18
        • 1970-01-01
        相关资源
        最近更新 更多