无法使用 alamofire 打印 json 值
unable to print json value with alamofire
我正在尝试使用 flickr API 获取数据,我使用 Alamofire 和 swiftyJSON 编写了一个简单的代码来从 flickr 获取这些数据,但是我能够打印数据大小,但是当我尝试打印json,我的 catch
块运行。我的代码如下所示
func getPhotos(completion: @escaping CompletionHandler) -> Void {
let parameter: [String: Any] = [
"method": PHOTOS_METHOD,
"api_key": FLICKR_API_KEY,
"per_page": PER_PAGE,
"page": PAGE,
"format": FORMAT_TYPE,
"nojsoncallback": JSON_CALLBACK]
Alamofire.request(FLICKR_URL, method: .get, parameters: parameter, encoding: JSONEncoding.default, headers: HEADER).responseString { (response) in
if response.result.error == nil {
guard let data = response.data else {return}
do {
if let json = try JSON(data: data).array {
print(json)
}
completion(true)
} catch {
print("eroorrrre")
completion(false)
}
print("CALL CORRECT")
print(data)
completion(true)
}
else {
completion(false)
debugPrint(response.result.error as Any)
}
}
}
我的控制台日志
eroorrrre
CALL CORRECT
128 bytes
我不确定我在这里做错了什么,任何帮助都会得到帮助
试试这个
Alamofire.request(FLICKR_URL, method: .get, parameters: parameter, headers: HEADER).responseJSON { response in // call responseJSON instead of responseString
if response.result.isSuccess { // If http request is success
let json: JSON = JSON(response.result.value!) // JSON format from SwiftyJSON (I suppose you are using it)
guard let data = json.array else { // You suppose that json is array of objects
print("Unexpected JSON format")
completion(false)
}
print(data)
completion(true)
} else {
print(response.error)
completion(false)
}
}
我正在尝试使用 flickr API 获取数据,我使用 Alamofire 和 swiftyJSON 编写了一个简单的代码来从 flickr 获取这些数据,但是我能够打印数据大小,但是当我尝试打印json,我的 catch
块运行。我的代码如下所示
func getPhotos(completion: @escaping CompletionHandler) -> Void {
let parameter: [String: Any] = [
"method": PHOTOS_METHOD,
"api_key": FLICKR_API_KEY,
"per_page": PER_PAGE,
"page": PAGE,
"format": FORMAT_TYPE,
"nojsoncallback": JSON_CALLBACK]
Alamofire.request(FLICKR_URL, method: .get, parameters: parameter, encoding: JSONEncoding.default, headers: HEADER).responseString { (response) in
if response.result.error == nil {
guard let data = response.data else {return}
do {
if let json = try JSON(data: data).array {
print(json)
}
completion(true)
} catch {
print("eroorrrre")
completion(false)
}
print("CALL CORRECT")
print(data)
completion(true)
}
else {
completion(false)
debugPrint(response.result.error as Any)
}
}
}
我的控制台日志
eroorrrre
CALL CORRECT
128 bytes
我不确定我在这里做错了什么,任何帮助都会得到帮助
试试这个
Alamofire.request(FLICKR_URL, method: .get, parameters: parameter, headers: HEADER).responseJSON { response in // call responseJSON instead of responseString
if response.result.isSuccess { // If http request is success
let json: JSON = JSON(response.result.value!) // JSON format from SwiftyJSON (I suppose you are using it)
guard let data = json.array else { // You suppose that json is array of objects
print("Unexpected JSON format")
completion(false)
}
print(data)
completion(true)
} else {
print(response.error)
completion(false)
}
}