2017-07-12 28 views
-3

如何获取JSON数据并将其添加到URL?如何在Swift中获取JSON数据并将其添加到URL

我的情况是我会访问JSON URL,但我需要从其他JSON URL获得值Token。

这是我的Json,我需要一个令牌来访问它。 https://api-sandbox.tiket.com/flight_api/all_airport/token= ......

要获得令牌,我必须从其他JSON访问,这种观点在其他JSON获得令牌。

{ “diagnostic”:{“status”:200,“elapsetime”:“0.2082”,“memoryusage”:“16.99MB”,“unix_timestamp”:1499832598,“confirm”:“success” “:” ID “ ”货币“: ”IDR“}, ”output_type“: ”JSON“, ”login_status“: ”假“, ”令牌“:” b650fce732668b58b0a4c404b65932e972ad123d“ }

我必须获得价值代币,并将其添加到第一个JSON网址以访问第一个JSON

This my code:

FUNC getLatestKotaTujuan(){

var tujuanUrl = URLComponents(string: "https://api-sandbox.tiket.com/flight_api/all_airport?")! 
    tujuanUrl.queryItems = [ 
     URLQueryItem(name: "token", value: "4d4bba355bb920fbdc1aa3848769a59ba74ea03c"), 
     URLQueryItem(name: "output", value: "json") 
    ] 

    let request = tujuanUrl.url 
    let task = URLSession.shared.dataTask(with: request!, completionHandler: { (data, response, error) -> Void in 

     if let error = error { 
      print(error) 
      return 
     } 

     // Parse JSON data 
     if let data = data { 
      self.kotaTujuan = self.parseJsonData(data: data) 

      // Reload table view 
      OperationQueue.main.addOperation({ 
       self.tableView.reloadData() 
      }) 
     } 
    }) 

    task.resume() 
} 

回答

0

您可以使用Alamofire & SwiftyJSON

  1. 安装两个cocopod

    pod 'Alamofire' 
        pod 'SwiftyJSON' 
    
  2. 我的JSON是像

    {"loginNodes":[{"errorMessage":"Welcome To Alamofire","name":Enamul Haque,"errorCode":"0","photo":null}]} 
    
  3. 声明多维数组

    var arrRes = [[String:AnyObject]]() 
    
  4. Alamofire得到JSON从服务器

    func getList() { 
    
    
        Alamofire.request("url.....", method: .post, parameters: [ 
        "userNameid":"25" 
    
        ]).responseJSON{(responseData) -> Void in 
         if((responseData.result.value != nil)){ 
    
          // let jsonData = JSON(responseData.result.value) 
          HUD.hide(); 
    
          if((responseData.result.value != nil)){ 
           let swiftyJsonVar = JSON(responseData.result.value!) 
    
           if let resData = swiftyJsonVar["loginNodes"].arrayObject { 
            self.arrRes = resData as! [[String:AnyObject]] 
           } 
    
           if self.arrRes.count > 0 { 
            self.tableView.reloadData() 
           } 
    
          } 
         } 
        } 
    
        } 
    
  5. 集数据表视图使用像

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    
        return arrRes.count 
    } 
    
    
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Agent_Account_Balance_Cell 
    
         cell.namelabel.text = arrRes[indexPath.row]["name"] as? String 
    
         return cell 
        } 
    
相关问题