如何从网址获取网络服务数据?

时间:2017-08-02 06:18:42

标签: ios json swift3

在这个网络服务中,我需要获取数据并显示image中提到的任何人都可以帮助我如何传递这种类型网址我不知道如何传递这种类型的网络服务得到和网址是

 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()
    }

2 个答案:

答案 0 :(得分:0)

我已尝试过您的代码,以下解决方案正在运行。

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()
}

答案 1 :(得分:0)

试试这个:

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)

      }
  }
相关问题