Swift 解析JSON-Alamofire
我正在尝试使用Rest Web服务 我的JSON响应结构是:Swift 解析JSON-Alamofire,swift,alamofire,Swift,Alamofire,我正在尝试使用Rest Web服务 我的JSON响应结构是: "page": 1, "results": [ { "poster_path": "/9Hj2bqi955SvTa5zj7uZs6sic29.jpg", "adult": false, "overview": "", "release_date": "2015-03-15", "genre_ids": [ 99 ], "id
"page": 1,
"results": [
{
"poster_path": "/9Hj2bqi955SvTa5zj7uZs6sic29.jpg",
"adult": false,
"overview": "",
"release_date": "2015-03-15",
"genre_ids": [
99
],
"id": 441580,
"original_title": "The Jinx: The Life and Deaths of Robert Durst Season 1 Chapter 6: What the Hell Did I Do?",
"original_language": "en",
"title": "The Jinx: The Life and Deaths of Robert Durst Season 1 Chapter 6: What the Hell Did I Do?",
"backdrop_path": "/3br0Rt90AkaqiwVBZVvVUYD1juQ.jpg",
"popularity": 1,
"vote_count": 1,
"video": false,
"vote_average": 10
}
],
"total_results": 307211,
"total_pages": 15361
}
我正在尝试获取页面和结果数组…但在解析后,页面(paginationCount)和结果(jsonArray)变量为零
这是我的密码:
Alamofire.request(ConstantHelper.kUrlDiscoverMovies, method: .get, parameters: ["api_key": ConstantHelper.kApiKey, "certification" : average, "sort_by" : "vote_average.desc" ]).validate()
.responseJSON { response in
switch response.result {
case .success:
if let repoJSON = response.result.value as? JSON {
let jsonArray = repoJSON["results"] as? NSMutableArray
for item in jsonArray! {
guard let movie = Movie(json: item as! JSON) else
{
print("Issue deserializing model")
return
}
listMovies.append(movie)
}
if let paginationCount = repoJSON["total_pages"] as? String {
completion(listMovies, Int(paginationCount)!, nil)
}
else {
completion(listMovies, 0, nil)
}
}
break
case .failure(let error):
completion(nil, 0, error as NSError?)
break
}
}
您可以使用一些本机Swift方法(无需使用alarmofire)轻松解析json,如果您有post请求,您可以使用一些post参数使用该函数 PS:a just是标记值的列表,标记在大多数情况下是字符串,值的类型可以是anyObject,甚至可以是列表或字典。为此,您需要将从服务器获得的数据调整为您已初始化的变量
func parseMyJSon(_ username: String, password: String) {
var request = URLRequest(url: URL(string: "https://myurlHere")!) // if you have a http please enable the http connection
request.httpMethod = "POST" // if you have a get request just change it to get
// the post parameters
let postString = "user_name=\(username)&password=\(password)"
// Get the request :(get the json)
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil && data != nil else {
print("error=\(error)")
// Do something
return
}
print("the data we have got from the server : \(data!)")
// Do Something
// think about casting your data to NSDictionnary<String,anyObject> in your case or NSDictionnary<String,[String]> // if your values contains a list of Strings
})
task.resume()
}
func parseMyJSon(u用户名:字符串,密码:字符串){
var request=URLRequest(url:url(字符串):https://myurlHere“”!)//如果您有http连接,请启用http连接
request.httpMethod=“POST”//如果您有get请求,只需将其更改为get即可
//post参数
让postString=“用户名=\(用户名)&密码=\(密码)”
//获取请求:(获取json)
request.httpBody=postString.data(使用:String.Encoding.utf8)
让task=URLSession.shared.dataTask(其中:request作为URLRequest,completionHandler:{data,response,中的错误
保护错误==nil&&data!=nil else{
打印(“错误=\(错误)”)
//做点什么
返回
}
打印(“我们从服务器获得的数据:\(数据!)”)
//做点什么
//在您的案例中,考虑将数据强制转换为NSDictionnary,或者如果值包含字符串列表,则考虑将数据强制转换为NSDictionnary//
})
task.resume()
}
您可以使用一些本机Swift方法轻松解析json(无需使用alarmofire),如果您有post请求,您可以使用一些post参数使用该函数
PS:a just是标记值的列表,标记在大多数情况下是字符串,值的类型可以是anyObject,甚至可以是列表或字典。为此,您需要将从服务器获得的数据调整为您已初始化的变量
func parseMyJSon(_ username: String, password: String) {
var request = URLRequest(url: URL(string: "https://myurlHere")!) // if you have a http please enable the http connection
request.httpMethod = "POST" // if you have a get request just change it to get
// the post parameters
let postString = "user_name=\(username)&password=\(password)"
// Get the request :(get the json)
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil && data != nil else {
print("error=\(error)")
// Do something
return
}
print("the data we have got from the server : \(data!)")
// Do Something
// think about casting your data to NSDictionnary<String,anyObject> in your case or NSDictionnary<String,[String]> // if your values contains a list of Strings
})
task.resume()
}
func parseMyJSon(u用户名:字符串,密码:字符串){
var request=URLRequest(url:url(字符串):https://myurlHere“”!)//如果您有http连接,请启用http连接
request.httpMethod=“POST”//如果您有get请求,只需将其更改为get即可
//post参数
让postString=“用户名=\(用户名)&密码=\(密码)”
//获取请求:(获取json)
request.httpBody=postString.data(使用:String.Encoding.utf8)
让task=URLSession.shared.dataTask(其中:request作为URLRequest,completionHandler:{data,response,中的错误
保护错误==nil&&data!=nil else{
打印(“错误=\(错误)”)
//做点什么
返回
}
打印(“我们从服务器获得的数据:\(数据!)”)
//做点什么
//在您的案例中,考虑将数据强制转换为NSDictionnary,或者如果值包含字符串列表,则考虑将数据强制转换为NSDictionnary//
})
task.resume()
}
我不知道JSON是什么类型的
但是keyresults
的值是[[String:Any]]
,从不NSMutableArray
let jsonArray = repoJSON["results"] as? [[String:Any]]
键总页数
的值是Int
而不是String
(没有双引号)
我不知道JSON是什么类型的
但是keyresults
的值是[[String:Any]]
,从不NSMutableArray
let jsonArray = repoJSON["results"] as? [[String:Any]]
键总页数
的值是Int
而不是String
(没有双引号)
解析之后?哪里在成功闭包内,它有效吗?在成功闭包内。。。解析后是否让paginationCount为零?哪里在成功闭包内,它有效吗?在成功闭包内。。。让paginationCount为零