【问题标题】:How get JSON Data in Swift and add it to URL如何在 Swift 中获取 JSON 数据并将其添加到 URL
【发布时间】:2017-07-12 04:05:21
【问题描述】:

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

我的情况是我将访问 JSON URL,但我需要来自其他 JSON URL 的 value Token。

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

要获取token,我必须从其他json访问,这个查看其他json获取token。

{ "诊断":{"状态":200,"elapsetime":"0.2082","memoryusage":"16.99MB","unix_timestamp":1499832598,"confirm":"success","lang":"id", "货币":"印尼盾"}, “输出类型”:“json”, “登录状态”:“假”, “令牌”:“b650fce732668b58b0a4c404b65932e972ad123d” }

我必须获取值令牌,并将其添加到第一个 JSON URL 以访问第一个 JSON

这是我的编码:

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

【问题讨论】:

    标签: ios json swift


    【解决方案1】:

    你可以使用 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. 使用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
           }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      • 2016-08-27
      • 2022-07-28
      • 2013-02-08
      相关资源
      最近更新 更多