【问题标题】:Alamofire JSON Response Error with post parameter带有 post 参数的 Alamofire JSON 响应错误
【发布时间】:2020-11-03 19:29:09
【问题描述】:

我用的是alamofire和swityjson,虽然我用的方法一样,但是这里没有得到任何结果。

let exampleURl = URL(string: exampleUrl)!
let params: [String: String] = ["id": "expampleString"]
let headers: HTTPHeaders = [
    "charset": "UTF-8",
    "Accept": "application/json"
]
       Alamofire.request(exampleURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).validate(statusCode: 200..<600).responseJSON() { response
           in
           
        switch response.result {
           case.success:
               if let json = response.data {
                    do{
                     let data = try JSON(data: json)
                     let str = data
                   
                     print(str["arrayName"])
                     let arrayData = str["arrayName"].arrayValue.map{$0["content"].stringValue}
                     print(arrayData[0])
                     let credit = arrayData[0]
                                   }
                                   catch{
                                   print("JSON Error")
                                   }
                      }
                  case .failure(let error):
                      print("RESPONSE ERROR: \(error)")
                  }
           }

这是我的 Json 输出。

{"arrayName":[{"content":"Hello_World"}]}

这是错误。我不明白。我发送了 post 参数,但我无法在 Json 数组中获取参数。

RESPONSE ERROR: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

【问题讨论】:

    标签: json swift xcode alamofire response


    【解决方案1】:

    看来你用错了alamofire,请尝试一下:

    这是请求:

    let url = URL(string: "YOUR LINK HERE")
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in  
        let jsonDecoder = JSONDecoder()
        let responseModel = try jsonDecoder.decode(BaseModel.self, from: data!)
    
    }
    task.resume()
    

    这是您的 swift 模型类:

    import Foundation
    struct ArrayName : Codable {
        let content : String?
    
        enum CodingKeys: String, CodingKey {
    
            case content = "content"
        }
    
        init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: CodingKeys.self)
            content = try values.decodeIfPresent(String.self, forKey: .content)
        }
    
    }
    
    struct BaseModel : Codable {
        let arrayName : [ArrayName]?
    
        enum CodingKeys: String, CodingKey {
    
            case arrayName = "arrayName"
        }
    
        init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: CodingKeys.self)
            arrayName = try values.decodeIfPresent([ArrayName].self, forKey: .arrayName)
        }
    
    }
    

    【讨论】:

    • 并且不要忘记将帖子正文添加到您的请求中
    【解决方案2】:

    该错误通常表明您没有收到 JSON 响应。您需要调试该响应,通常通过将其打印为 String 或在响应处理程序中设置断点并在那里检查它。

    【讨论】:

      猜你喜欢
      • 2017-12-12
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 1970-01-01
      相关资源
      最近更新 更多