Ios 使用Alamofire调用JSONArray

Ios 使用Alamofire调用JSONArray,ios,swift3,alamofire,Ios,Swift3,Alamofire,我还是iOS开发的初学者。我在我的项目中使用Swift 3、Alamofire 4和SwiftyJson。我尝试从JSON获取数据。JSON在单个数组中返回。这是报税表: JSON: [{ "id": "1", "post_title": "Event_1", "post_content": "Taylor Swift", "start_date": "2016-11-23", "end_date": "2016-11-23", "post_dat

我还是iOS开发的初学者。我在我的项目中使用Swift 3、Alamofire 4和
SwiftyJson
。我尝试从JSON获取数据。JSON在单个数组中返回。这是报税表:

JSON:

[{
    "id": "1",
    "post_title": "Event_1",
    "post_content": "Taylor Swift",
    "start_date": "2016-11-23",
    "end_date": "2016-11-23",
    "post_date": "2016-11-18"
}, {
    "id": "2",
    "post_title": "Event_2",
    "post_content": "Nickelback",
    "start_date": "2016-11-15",
    "end_date": "2016-11-16",
    "post_date": "2016-11-15"
}, {
    "id": "3",
    "post_title": "Event_3",
    "post_content": "Nirvana",
    "start_date": "10.00pm on 22-02-2012",
    "end_date": "6.00am on 23-02-2012",
    "post_date": "2016-07-10"
}]
var newsArray : [[String:Any]] = []

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return newsArray.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell

    let currentNews = newsArray[indexPath.row]
    if let post_content = currentNews["post_content"] {
        cell.lbl_content.text = post_content as? String
    }

    return cell
}    


func getJSON(){

    Alamofire.request(BASE_URL).responseJSON {
        response in

        switch response.result {
        case .success(let value):
            let json = JSON(value)
            print(json)

            if let tempArray = json as? [[String: Any]] {
                self.newsArray = tempArray
                self.tableView.reloadData()
            }

        case .failure(let error):
            print(error)
        }
    }
}
Swift:

[{
    "id": "1",
    "post_title": "Event_1",
    "post_content": "Taylor Swift",
    "start_date": "2016-11-23",
    "end_date": "2016-11-23",
    "post_date": "2016-11-18"
}, {
    "id": "2",
    "post_title": "Event_2",
    "post_content": "Nickelback",
    "start_date": "2016-11-15",
    "end_date": "2016-11-16",
    "post_date": "2016-11-15"
}, {
    "id": "3",
    "post_title": "Event_3",
    "post_content": "Nirvana",
    "start_date": "10.00pm on 22-02-2012",
    "end_date": "6.00am on 23-02-2012",
    "post_date": "2016-07-10"
}]
var newsArray : [[String:Any]] = []

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return newsArray.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell

    let currentNews = newsArray[indexPath.row]
    if let post_content = currentNews["post_content"] {
        cell.lbl_content.text = post_content as? String
    }

    return cell
}    


func getJSON(){

    Alamofire.request(BASE_URL).responseJSON {
        response in

        switch response.result {
        case .success(let value):
            let json = JSON(value)
            print(json)

            if let tempArray = json as? [[String: Any]] {
                self.newsArray = tempArray
                self.tableView.reloadData()
            }

        case .failure(let error):
            print(error)
        }
    }
}
问题:

[{
    "id": "1",
    "post_title": "Event_1",
    "post_content": "Taylor Swift",
    "start_date": "2016-11-23",
    "end_date": "2016-11-23",
    "post_date": "2016-11-18"
}, {
    "id": "2",
    "post_title": "Event_2",
    "post_content": "Nickelback",
    "start_date": "2016-11-15",
    "end_date": "2016-11-16",
    "post_date": "2016-11-15"
}, {
    "id": "3",
    "post_title": "Event_3",
    "post_content": "Nirvana",
    "start_date": "10.00pm on 22-02-2012",
    "end_date": "6.00am on 23-02-2012",
    "post_date": "2016-07-10"
}]
var newsArray : [[String:Any]] = []

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return newsArray.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell

    let currentNews = newsArray[indexPath.row]
    if let post_content = currentNews["post_content"] {
        cell.lbl_content.text = post_content as? String
    }

    return cell
}    


func getJSON(){

    Alamofire.request(BASE_URL).responseJSON {
        response in

        switch response.result {
        case .success(let value):
            let json = JSON(value)
            print(json)

            if let tempArray = json as? [[String: Any]] {
                self.newsArray = tempArray
                self.tableView.reloadData()
            }

        case .failure(let error):
            print(error)
        }
    }
}
看起来tempArray不包含任何值

我尝试在tableViewCell中实现该值

非常感谢您的帮助。

试试这个

if let tempArray = json as? [[String: Any]] {
                self.newsArray = tempArray
                self.tableView.reloadData()
            }
仅使用
json
不要
json[]

请在下面尝试

if let tempArray = json as? [[String: Any]] {
                self.newsArray = tempArray.mutableCopy()
                self.tableView.reloadData()
            }

尝试将json[]更改为?[[String:Any]到
json.array

您可以尝试此代码

Alamofire.request(BASE_URL).responseJSON {
    response in

    switch response.result {
    case .success(let value):
        //to get JSON return value
        if let result = response.result.value {
            let JSON = result as! [[String: Any]]
            //then use guard to get the value your want

        }

case .failure(let error):
    print(error)
}
}


尝试上面的代码行。我可以帮你。

目前我没有使用Swift 3,Alamofire 4。我在下面的链接中找到了一些解决方案。请检查,如果您能从中找到任何解决方案。 链接:


打印(json)的值是多少?与问题中的json返回值相同。打印tempArray,您将了解数组是否为空,以及它的emptyIt错误。类型[[String:Any]]的值没有成员“mutableCopy”它错误:条件绑定的初始值设定项必须有可选类型,而不是“[JSON]”,然后将
arrayValue
更改为
array
仅用于检查nilNo打印(JSON)的tempArrayprint日志上的值?在打印(JSON)上有值,但在打印(tempArray)时有值是否为空?您可以将其粘贴到此处吗?
'NSArray'不能转换为“[[String:Any]]”
因此我改为
self.newsArray=tempArray as![[String:Any]]
。之后,
tempArray
仍然没有值。为什么它在jsonArray中返回所有值而不是jsonObject?还有一件事,为什么你没有使用(let value)?“为什么它在jsonArray中返回所有值而不是jsonObject?”-我也不知道,这取决于你的服务器实现,我读了你的json,它在根级别没有键,所以我确定这是一个jsonArray。“还有一件事,你为什么不使用(let值)?”-你是说我为什么使用guard而不是let?guard是一种快速语法,请正确阅读,json在根级别没有密钥。
response.result.value
value
之间有什么区别?看起来您正在从参数中获取值。如果联系人不存在,我应该放置什么?@Sendra请尝试使用此选项:if let resData=swiftyJsonVar.arrayObject