2016-11-22 74 views
0

我有以下JSon從服務器中檢索到的數據,我想提取值名稱並將它們作爲模型存儲在數組或字典中。問題是,返回自己的價值是以另一種字典的形式出現的。我如何提取頻率,描述和數量的值,並將它們更新到我的tableview。以下是我從服務器請求獲得的Json數據格式。我是新來的迅速和字典的概念和陣列是安靜的困惑,我從Swift iOS9中提取Json數據的值

{ 
"payment" =  (
      { 
     amount = 100; 
     currency = USD; 
     description = "Awesome Videos"; 
     frequency = Day; 
    }, 
      { 
     amount = 500; 
     currency = USD; 
     description = "Awesome Videos"; 
     frequency = Week; 
    }, 
      { 
     amount = 3000; 
     currency = USD; 
     description = "Awesome Videos"; 
     frequency = Months; 
    } 
); 

}

我想他們在本地存儲在字典或數組,並更新到我的tableview。

這裏也是我的代碼從服務器

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in 
     if (data != nil){ 
      print(data) 
      do { 

       // let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) 
       let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) 

       print(jsonResult) 
       print(jsonResult["payment_plans"]) 



       if let plan_list = jsonResult as? NSDictionary{ 
        print("got dictionary") 

        for (key, value) in plan_list 
        { 
         print(value) 
         print("printing keys") 
         print(key) 

         if let plans = value as? NSDictionary 
         { 
          print("printing values") 
          print(plans) 
          print(plans["currency"]) 
         } 
        } 
       } 

      } catch{ 
       print("Json Serialization failed") 
      } 
     } 

    } 
    /* 
    if (response != nil){ 
    print(response) 
    } 
    else{ 
    print(error) 
    } 
    } 
    */ 
    task.resume() 
} 

我堅持在這裏如何提取,我在字典中獲取值獲取數據。在此先感謝

回答

0

字典它是一個key-value存儲在您的情況下的值具有AnyObject類型。

將數據轉換成DictionaryArray可以使用

let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) 

然後創建支付數組

if let payments = jsonResult as? Array{ 

    *iterate here all payments 

} 

如果你想使用for (key, value) in plan_list你需要改變它的到for (key, value) in plan_list.enumerate()

+0

我設法得到Json數據....任何想法如何我可以填充數據到tableview?似乎不會在我身邊工作。謝謝 – Arnold

+0

在你的班級 –

+0

中創建一個屬性**付款**對不起,然後把所有數據都放到** payments **屬性中,並在表視圖中調用reloadData()dataSource方法,你可以使用'if let payment = self.payments如? NSDictionary {cell.label.text =付款[「描述」]爲?字符串}'不要忘記你也有一個整數! –

2

嗨您可以通過以下方式獲得您的結果:

if((jsonResult) != nil) { 
    let swiftyJsonVar = jsonResult! 
    do { 
     if let dicObj = swiftyJsonVar as? NSDictionary { 
      print("Response is dictionary") 
      print(dicObj) 
      let arrObj = dicObj["payment"] as NSArray 
      // Then iterate your arrObj and do as per your need. 
      //for eg. 
      arrObj[0]["description"] 
     } 
    } 
} 
+0

這將返回一個致命錯誤「意外地發現零,同時解開一個可選值」'dicObj [「payment」]'返回nil – Arnold

+0

你可以像這樣使用.. dicObj [「payment」] as! NSArray –

+0

我得到它的工作....謝謝你,你能推薦我如何可以在tableviewcell中填充上述數據?這看起來不適合我。這裏是我的代碼 'FUNC的tableView(的tableView:UITableView的,的cellForRowAtIndexPath indexPath:NSIndexPath) - !?>的UITableViewCell { 讓電池= tableView.dequeueReusableCellWithIdentifier( 「choosePlan」,forIndexPath:indexPath)爲UITableViewCell的 cell.textLabel的.text = PlanStorage [indexPath.row] 返回單元格 }' – Arnold