Ios 使用Alamofire时将JSON数据放入数组
我正在尝试从我的站点检索数据,我正在使用Alamofire,如何将这些数据放入一个数组中,以便填充我的表视图Ios 使用Alamofire时将JSON数据放入数组,ios,json,swift,alamofire,Ios,Json,Swift,Alamofire,我正在尝试从我的站点检索数据,我正在使用Alamofire,如何将这些数据放入一个数组中,以便填充我的表视图 override func viewDidLoad() { super.viewDidLoad() Alamofire.request("http://lytestech.ga/api/lytes/get_movies/").responseJSON { response in if let json = respons
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request("http://lytestech.ga/api/lytes/get_movies/").responseJSON { response in
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
let res = json as! NSDictionary
let movies = res["movies"] as! NSArray
// movieTitles = movies["movie_desc"]
let movieTitles: [String] = movies["movietitile"] as! String
print (movies)
print (movieTitles)
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
}
json数据
JSON: {
movies = (
{
id = 66;
"movie_desc" = "spiders bite";
movietitile = spiderman;
},
{
id = 64;
"movie_desc" = horror;
movietitile = mummy;
}
);
status = ok;
}
(
{
id = 66;
"movie_desc" = "spiders bite";
movietitile = spiderman;
},
{
id = 64;
"movie_desc" = horror;
movietitile = mummy;
}
)
将您的回答
数据
转换为字典
let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: response.data) as! [String : Any]
从字典
中提取你的数组
,这里有键名电影
if let arryMovies1 = dictionary["movies"] as? [[String:Any]] {
print (arryMovies1);
// Now your have your Array
// You can Populate into UItableView
// when your array is modified than you have to reload the tableData
self.arryMovies=arryMovies1
self.tableView.reloadData()
}
在列表视图中填充的单元格中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "you_cell_identifre") as! UITableCell
let dicMovie = self.arryMovies[indexPath.row] as! NSDictionary
cell.lableTitle.text = dicMovie.value(forKey: "movietitile") as? String
return cell
}
我希望这将帮助您将您的回答数据
转换为字典
let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: response.data) as! [String : Any]
var movies:[Dictionary<String,AnyObject>] = [] // Makes movies array global
movies = res["movies"] // and tableview reload
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier) ??
UITableViewCell(style: .default, reuseIdentifier: identifier)
cell.textLabel!.text = movies[indexPath.row]["movietitile"] as? String
return cell
}
从字典
中提取你的数组
,这里有键名电影
if let arryMovies1 = dictionary["movies"] as? [[String:Any]] {
print (arryMovies1);
// Now your have your Array
// You can Populate into UItableView
// when your array is modified than you have to reload the tableData
self.arryMovies=arryMovies1
self.tableView.reloadData()
}
在列表视图中填充的单元格中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "you_cell_identifre") as! UITableCell
let dicMovie = self.arryMovies[indexPath.row] as! NSDictionary
cell.lableTitle.text = dicMovie.value(forKey: "movietitile") as? String
return cell
}
我希望这将帮助您var movies:[Dictionary]=[]//使movies数组全球化
var movies:[Dictionary<String,AnyObject>] = [] // Makes movies array global
movies = res["movies"] // and tableview reload
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier) ??
UITableViewCell(style: .default, reuseIdentifier: identifier)
cell.textLabel!.text = movies[indexPath.row]["movietitile"] as? String
return cell
}
movies=res[“movies”]//并重新加载tableview
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让标识符=“单元格”
let cell=tableView.dequeueReusableCell(带标识符:标识符)??
UITableViewCell(样式:。默认值,reuseIdentifier:标识符)
cell.textLabel!.text=movies[indexPath.row][“movietile”]作为?字符串
返回单元
}
var movies:[字典]=[]//使movies数组全局化
movies=res[“movies”]//并重新加载tableview
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让标识符=“单元格”
let cell=tableView.dequeueReusableCell(带标识符:标识符)??
UITableViewCell(样式:。默认值,reuseIdentifier:标识符)
cell.textLabel!.text=movies[indexPath.row][“movietile”]作为?字符串
返回单元
}
在电影中存储电影NSArray
var movies = NSArray()
之后,在cellForRowAt
方法中,您可以这样显示数据
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier") as! YouCustomCell
let movieDetail = movies[indexPath.row] as! NSDictionary
cell.lblMovieName.text = movieDetail.value(forKey: "movietitile") as? String
return cell
}
注意:-这只是代码框架,您可以根据您的自定义单元格和插座使用将电影存储在电影NSArray
var movies = NSArray()
之后,在cellForRowAt
方法中,您可以这样显示数据
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier") as! YouCustomCell
let movieDetail = movies[indexPath.row] as! NSDictionary
cell.lblMovieName.text = movieDetail.value(forKey: "movietitile") as? String
return cell
}
注意:-这只是代码框架,您可以根据自定义单元格和插座使用
func演示()
{
让str:String=”http://lytestech.ga/api/lytes/get_movies/"
让url=NSURL(字符串:str)
let request=NSMutableURLRequest(URL:URL!)
request.HTTPMethod=“POST”
request.setValue(“application/json”,forHTTPHeaderField:“Content Type”)
Alamofire.request(请求)
.responseString{response in
开关(response.result){
case.Success(让JSON):
让data=JSON.dataUsingEncoding(NSUTF8StringEncoding)!
做{
让responseString=尝试NSJSONSerialization.JSONObjectWithData(数据,选项:[])作为?[String:AnyObject]
//打印(responseString!)
self.movieName=[]
self.desc=[]
让arr=responseString![“电影”]as!NSArray
对于j,0..
func演示()
{
让str:String=”http://lytestech.ga/api/lytes/get_movies/"
让url=NSURL(字符串:str)
let request=NSMutableURLRequest(URL:URL!)
request.HTTPMethod=“POST”
request.setValue(“application/json”,forHTTPHeaderField:“Content Type”)
Alamofire.request(请求)
.responseString{response in
开关(response.result){
case.Success(让JSON):
让data=JSON.dataUsingEncoding(NSUTF8StringEncoding)!
做{
让responseString=尝试NSJSONSerialization.JSONObjectWithData(数据,选项:[])作为?[String:AnyObject]
//打印(responseString!)
self.movieName=[]
self.desc=[]
让arr=responseString![“电影”]as!NSArray
对于0中的j,您可以使用SwiftyJSON来完成此操作:您知道如何从数组中填充tableview吗?是的,我知道如何@rakithen创建数组类型的实例变量,将movies数据存储到该数组中,并将该数组传递给tableview的datasource方法来填充它。@gideonlytes您可以使用SwiftyJSON完成此操作:您知道吗如何从数组填充tableview?是的,我知道如何@rakithen创建数组类型的实例变量,将电影数据存储到该数组中,并将该数组传递给tableview的datasource方法以填充它。@GideonLytes