2016-12-01 59 views
-1

我不明白如何从字典数组,我曾尝试在此代码,但我希望得到阵列只有内容和类型如何从字典数组中迅速3

这是我的数组

{ 
    wsResponse =  { 

    aarti =(
    { 
     content = ""; 
     identifier = "slok_one"; 
     title = 1; 
     type = "\U092d\U0915\U094d\U0924\U093e\U092e\U0930"; 
    }, 
      { 
     content = ""; 
     identifier = "slok_two"; 
     title = 2; 
     type = "\U092d\U0915\U094d\U0924\U093e\U092e\U0930"; 
    }, 
      { 
     content = ""; 
     identifier = "slok_three"; 
     title = 3; 
     type = "\U092d\U0915\U094d\U0924\U093e\U092e\U0930"; 
    } 

} 
); 
}; 

这里是我的代码

Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default) 
     .responseJSON { response in 

      if let status = response.response?.statusCode { 
       switch(status){ 
       case 201: 
        print("example success") 
       default: 
        print("error with response status: \(status)") 
       } 
      } 

      //to get JSON return value 
      if let array = response.result.value 
      { 
       let JSON = array as! NSDictionary 
       print("\(JSON)") 


       let response = JSON["wsResponse"] as! NSDictionary 
       let data = response["aarti"] 
      } 
} 

我想阵从此,像内容,名称,类型

预先感谢您

+0

对不起它的字典,但我希望像数组内容,名称,类型我得到导致在正确的字典 – johnyDiOS

+0

@EricAya **这是我的数组**不是结果它是我的真实数组,并且我想从这个 – johnyDiOS

+1

数组当中得到数组内容! NSDictionary'非常非常混乱! – vadian

回答

2

根据该打印aarti阵列中的所有值的JSON:

if let JSON = response.result.value as? [String:Any] { 
    if let response = JSON["wsResponse"] as? [String:Any], 
     let aarti = response["aarti"] as? [[String:Any]] { 

     for item in aarti { 
      let content = item["content"] as! String 
      let identifier = item["identifier"] as! String 
      let title = item["title"] as! Int 
      let type = item["type"] as! String 
      print(content, identifier, title, type) 
     } 
    } 
} 

基本上不斯威夫特使用NSDictionary/NSArray

如果要将所有content值放入数组中,请使用flatMap。该行可以替代整个重复循环。

let contentArray = aarti.flatMap { $0["content"] as? String } 

PS:如果你要创建的值多个阵列不这样做:使用自定义结构或类

+0

谢谢@vadian一个更多的问题如何使这个像contentArray阵列(所有内容值数组) – johnyDiOS

+0

我更新了答案。 – vadian