Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Alamofire Swift 3和Xcode 8 beta解析JSON时没有数据_Ios_Json_Swift_Xcode - Fatal编程技术网

Ios 使用Alamofire Swift 3和Xcode 8 beta解析JSON时没有数据

Ios 使用Alamofire Swift 3和Xcode 8 beta解析JSON时没有数据,ios,json,swift,xcode,Ios,Json,Swift,Xcode,我试图通过Alamofire从Swift 3中的url获取数据。手机上没有数据。我知道我在某个地方搞砸了,但我对swift 3相当陌生,看不到问题所在,代码如下: JSON结构。 { posts: 
 [ 
 { id: “000000”, url: "/content/interview", date: "2016-11-03 09:01:41", modified: "2016-11-

我试图通过Alamofire从Swift 3中的url获取数据。手机上没有数据。我知道我在某个地方搞砸了,但我对swift 3相当陌生,看不到问题所在,代码如下:

JSON结构。

{
        posts: 
    [
        
    {
        id: “000000”,
        url: "/content/interview",
        date: "2016-11-03 09:01:41",
        modified: "2016-11-03 09:03:47",
        title: "An interview",
        summary: 
    {
        value: "<p>Lorem ipsum is simply dummy text</p> ",
        format: "filtered_html"
        }
    ]   
}   
{
帖子:
    [

    {
id:“000000”,
url:“/content/interview”,
日期:“2016-11-03 09:01:41”,
修改:“2016-11-03 09:03:47”,
标题:“采访”,
总结:
    {
值:“Lorem ipsum只是虚拟文本”

”, 格式:“过滤的html” } ] }
Swift:

import UIKit
import Alamofire


class TableViewController: UITableViewController {


var titles = [String]()

var mainURL = "https://www.testapi.com"

typealias JSONstandard = [String : AnyObject]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    callAlamo(url: mainURL)
}

func callAlamo(url : String){
    Alamofire.request(url).responseJSON(completionHandler: {
        response in

        self.parseData(JSONData: response.data!)


    })

}

func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard

         print(readableJSON)
        if let post = readableJSON["posts"] as? JSONstandard {
            if let posts = post["posts"] {
                for i in 0..<posts.count {
                    let post = posts[i] as! JSONstandard

                    let title = post["title"] as! String
                    titles.append(title)

                    self.tableView.reloadData()

                }
            }

        }

    }


    catch {
        print(error)
    }


}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

    cell?.textLabel?.text = titles[indexPath.row]

    return cell!
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
导入UIKit
进口阿拉莫菲尔
类TableViewController:UITableViewController{
变量标题=[String]()
var mainURL=”https://www.testapi.com"
typealias JSONstandard=[字符串:AnyObject]
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
callAlamo(url:mainURL)
}
func callAlamo(url:String){
Alamofire.request(url).responseJSON(completionHandler:{
回应
self.parseData(JSONData:response.data!)
})
}
func parseData(JSONData:Data){
做{
var readableJSON=try JSONSerialization.jsonObject(使用:JSONData,选项:.mutableContainers)作为!JSONstandard
打印(可读JSON)
如果让post=readableJSON[“posts”]as?json标准{
如果让posts=post[“posts”]{
对于0..Int中的i{
返回标题。计数
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带标识符:“cell”)
单元格?.textLabel?.text=标题[indexPath.row]
返回单元!
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}

上述内容不会将任何数据带入“单元格”。如有任何帮助,我们将不胜感激。

首先,Swift 3中的JSON字典是
[String:Any]

typealias JSONstandard = [String : Any]
本教程的作者应将其命名为
JSONdictionary
,以使其更清晰

posts
的值是一个数组,JSON集合对象很容易识别:

  • []
    是数组
  • {}
    是字典


注:在Swift(1-3)中,通过
。可变容器
是非常无用的。

您的问题可能就在这里

     if let posts = post["posts"] {
                ---
            }
在前一行中,您通过readableJSON[“posts”]获得posts。Post没有名为“posts”的节点

请尝试以下代码:

     if let allPost = readableJSON["posts"] {
             var posts = allPost as! NSArray
                for post in posts {
                    let title = post["title"] as! String
                    titles.append(title)
                    self.tableView.reloadData()
                }
            }

添加
UITableViewDataSource
methode:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1 
}
不要太频繁地重新加载数据()

func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard

        print(readableJSON)
        if let post = readableJSON["posts"] as? JSONstandard {
            if let posts = post["posts"] {
                for i in 0..<posts.count {
                    let post = posts[i] as! JSONstandard
                    let title = post["title"] as! String
                    titles.append(title)               
                }
                self.tableView.reloadData() // moved out of loop
            }
        }
    }
    catch {
        print(error)
    }
}
func解析数据(JSONData:Data){
做{
var readableJSON=try JSONSerialization.jsonObject(使用:JSONData,选项:.mutableContainers)作为!JSONstandard
打印(可读JSON)
如果让post=readableJSON[“posts”]as?json标准{
如果让posts=post[“posts”]{

对于0中的i..请不要在Swift中建议
NSArray
。在这种情况下,问题将持续下去。谢谢,这非常有效!我想引入其他数据的原则大致相同。
func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard

        print(readableJSON)
        if let post = readableJSON["posts"] as? JSONstandard {
            if let posts = post["posts"] {
                for i in 0..<posts.count {
                    let post = posts[i] as! JSONstandard
                    let title = post["title"] as! String
                    titles.append(title)               
                }
                self.tableView.reloadData() // moved out of loop
            }
        }
    }
    catch {
        print(error)
    }
}