如何解析Web服务中的数据并将其传递给表视图?

时间:2017-07-18 04:06:25

标签: ios uitableview swift3

在这个我需要保存并将其传递给表视图的Web服务如何实现这一点,并且从json也无法获取数据,我需要在表视图中显示它,如下图所示,任何人都可以帮助我如何实施这个或任何建议也会有很大的帮助

enter image description here

我的代码如下所示

    let url = "http://www.json-generator.com/api/json/get/cwqUAMjKGa?indent=2"
    var detailsArray :[[String: AnyObject]] = []
    var titleName = [String]()
    var productName = [String]()
    var children = [String]()
    var childrenArray :[[String: AnyObject]] = []
    var productsArray :[[String:AnyObject]] = []
    var name = [String]()


    func downloadJsonWithURL() {
        let url = NSURL(string: self.url)
        URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
            if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
                self.detailsArray = (jsonObj!.value(forKey: "data") as? [[String: AnyObject]])!
                print(self.detailsArray)
                for item in self.detailsArray{
                    if let detailDict = item as? NSDictionary {
                        if let name = detailDict.value(forKey: "name"){
                            self.titleName.append(name as! String)
                            print(self.productName)
                        }
                        self.childrenArray = (detailDict.value(forKey: "children") as? [[String : AnyObject]])!
                        for item in self.childrenArray {
                            if let detailDict = item as? NSDictionary {
                                if let name = detailDict.value(forKey: "name"){
                                    self.productName.append(name as! String)
                                }
                                self.productsArray = (detailDict.value(forKey: "products") as? [[String : AnyObject]])!
                                for item in self.productsArray{
                                    if let detailDict = item as? NSDictionary {
                                        if let name = detailDict.value(forKey: "name"){
                                            self.name.append(name as! String)
                                            print(self.name)
                                        }
                                    }
                                }
                            }

                        }

                    }
                }
                OperationQueue.main.addOperation({
                    print(self.productName)
                    print(self.titleName)
                    print(self.name)
                })
            }
        }).resume()
}

0 个答案:

没有答案