Ios 如何从url获取web服务数据?

Ios 如何从url获取web服务数据?,ios,json,swift3,Ios,Json,Swift3,在这个web服务中,我需要获取数据并显示,如中所述。有人能帮助我如何传递这种类型的url吗?我不知道如何传递这种类型的web服务来获取,url是 let url = "http://www.json-generator.com/api/json/get/cwqUAMjKGa?indent=2" var detailsArray :[[String: AnyObject]] = [] var titleName = [String]() var productName =

在这个web服务中,我需要获取数据并显示,如中所述。有人能帮助我如何传递这种类型的url吗?我不知道如何传递这种类型的web服务来获取,url是

 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]])!
                    print(self.childrenArray)
                    }
                }
                OperationQueue.main.addOperation({
                    print(self.productName)
                    print(self.titleName)
                    print(self.name)
                })
            }
        }).resume()
    }

我已经尝试了你的代码,下面的解决方案正在运行

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? [String:AnyObject] {
                    if let name = detailDict["name"]{
                        self.titleName.append(name as! String)
                        print(self.productName)
                    }
                    self.childrenArray = (detailDict["children"]as? [[String : AnyObject]])!
                    print(self.childrenArray)
                }
            }
            OperationQueue.main.addOperation({
                print(self.productName)
                print(self.titleName)
                print(self.name)
            })
        }
    }).resume()
}

我已经尝试了你的代码,下面的解决方案正在运行

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? [String:AnyObject] {
                    if let name = detailDict["name"]{
                        self.titleName.append(name as! String)
                        print(self.productName)
                    }
                    self.childrenArray = (detailDict["children"]as? [[String : AnyObject]])!
                    print(self.childrenArray)
                }
            }
            OperationQueue.main.addOperation({
                print(self.productName)
                print(self.titleName)
                print(self.name)
            })
        }
    }).resume()
}
试试这个:

self.detailsArray = (jsonObj.value(forKey: "data") as? [[String: AnyObject]])!
      print(self.detailsArray)
      for item in self.detailsArray{
        if let detailDict = item as? [String : AnyObject] {
          if let name = detailDict["name"]{
            self.titleName.append(name as! String)
            print(self.productName)
          }
          if let dictChildren = detailDict["children"]{
        if ((dictChildren as? [Any]) != nil){
          self.childrenArray = dictChildren as! [[String : AnyObject]];
        }
        print(self.childrenArray)

      }
  }
试试这个:

self.detailsArray = (jsonObj.value(forKey: "data") as? [[String: AnyObject]])!
      print(self.detailsArray)
      for item in self.detailsArray{
        if let detailDict = item as? [String : AnyObject] {
          if let name = detailDict["name"]{
            self.titleName.append(name as! String)
            print(self.productName)
          }
          if let dictChildren = detailDict["children"]{
        if ((dictChildren as? [Any]) != nil){
          self.childrenArray = dictChildren as! [[String : AnyObject]];
        }
        print(self.childrenArray)

      }
  }


你们从web服务得到响应了吗?我能够得到,但是否有可能将它传递到表视图,正如我在上图中所示@KKRocksonly我得到的一些数据,然后它将崩溃@KKRockscan你们告诉我你们的应用程序在哪里崩溃了?这里崩溃了。你们从web服务得到响应了吗?我能够得到,但有可能吗如上图所示,将其传递到表视图@KKRocksonly我正在获取一些数据,稍后它将崩溃@KKRockscan你告诉我你的应用程序在哪里崩溃了吗?在这里,它崩溃了,我成功获取了对象。@vamsi尝试我的答案。在子数组中,它在nslog@kkrocksys中显示为null,因为作为响应,子数组没有出现,所以如何若要在子数组中获取数据@KKRocksi已成功获取对象。@请尝试我的答案。在子数组中,它在nslog@kkrocksys中显示为null,因为作为响应,子数组不出现,因此如何在子数组@KKRocks中获取数据