Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

ios - How get JSON Data in Swift and add it to URL

How I Get JSON Data and add it to URL?

My Case is I Will access JSON URL, but i need value Token from other JSON URL.

This My Json, i need a token to access it. https://api-sandbox.tiket.com/flight_api/all_airport/token=......

To get token, i must access from other JSON, this view the other JSON to get token.

{ "diagnostic":{"status":200,"elapsetime":"0.2082","memoryusage":"16.99MB","unix_timestamp":1499832598,"confirm":"success","lang":"id","currency":"IDR"}, "output_type":"json", "login_status":"false", "token":"b650fce732668b58b0a4c404b65932e972ad123d" }

I must get value token, and add it to first JSON URL to access First JSON

This my coding :

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()
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Alamofire & SwiftyJSON

  1. Install two cocopod

       pod 'Alamofire'
       pod 'SwiftyJSON'
    
  2. My Json is look like

    {"loginNodes":[{"errorMessage":"Welcome To Alamofire","name":Enamul Haque,"errorCode":"0","photo":null}]}
    
  3. Declare multidimensional array

     var arrRes = [[String:AnyObject]]()
    
  4. Alamofire get json from server

     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. set data to table view using like

       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
         }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...