2017-02-04 116 views
0

我有以下的数组是从一个API调用传递从PHP脚本:为什么我不能在Swift中访问这个数组的第二层?

["playerForm": { 
1 =  (
      { 
     date = "2017-01-31"; 
     name = Dicky; 
     result = L; 
     "results_id" = 42; 
    }, 

      { 
     date = "2016-12-24"; 
     name = Dicky; 
     result = W; 
     "results_id" = 19; 
    } 
); 
2 =  (
      { 
     date = "2017-01-25"; 
     name = G; 
     result = W; 
     "results_id" = 38; 
    }, 
      { 
     date = "2017-01-25"; 
     name = G; 
     result = D; 
     "results_id" = 40; 
    },  
      { 
     date = "2017-01-21"; 
     name = G; 
     result = L; 
     "results_id" = 34; 
    } 
); 
3 =  (
      { 
     date = "2017-01-31"; 
     name = Sultan; 
     result = W; 
     "results_id" = 42; 
    }, 

      { 
     date = "2017-01-15"; 
     name = Sultan; 
     result = L; 
     "results_id" = 30; 
    } 
); 
}] 

但是我似乎无法访问1,2,3部分吧...

let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject] 
        print (json!) 

        if let dict = json?["playerForm"] as? [String:AnyObject] { 
         print ("step 1") 
         if let arr = dict["1"] as? [[String:String]] { 


          print ("step 2") 

          self.leagueForm = arr.flatMap { Form($0) } 
          print (self.leagueForm) 
          print (self.leagueForm.count) 

          for i in 0..<self.leagueForm.count { 
           let form = self.leagueForm[i] 
           print (form.player_results!) 
           self.formGuide.append(form.player_results!) 

          } 

          print ("now") 
          print (self.formGuide) 
          self.resultsDisplay.results = self.formGuide 
          self.resultsDisplay.results = self.resultsDisplay.results.reversed() 
          self.resultsDisplay.displayResults() 

         } 

        } 

使用上面的代码,它只能得到第1步。

我在做什么错?

与PROGRSS *

let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject] 
        print (json!) 

        if let dict = json?["playerForm"] as? [String:AnyObject] { 
         print ("step 1") 

         for (_ , value) in dict { 
          if let arr = value as? [[String:Any]] { 
           print(arr) 
           //your code 
          } 
         } 


        } 

自定义结构更新,以定义阵列:

struct Form { 
    var player_result: String? 
    var player_name: String? 
    var result_date: String? 
    var result_id: String? 

    init(_ dictionary: [String : Any]) { 
     self.player_result = dictionary["result"] as? String ?? "" 
     self.player_name = dictionary["name"] as? String ?? "" 
     result_date = dictionary["date"] as? String ?? "" 
     result_id = String(dictionary["results_id"] as? Int ?? 0) 

    } 
} 

回答

1

更改您的数组类型[[String:String]][[String:Any]],因为它同时包含StringNumber的价值。

if let arr = dict["1"] as? [[String:Any]] { 
    print(arr) 
    //your code 
} 

注:您需要通过dict字典,因为它的每个按键阵列具有循环。

for (_ , value) in dict { 
    if let arr = value as? [[String:Any]] { 
     print(arr) 
     //your code 
    } 
} 
+0

非常好!这是打印数组'arr'。然而,这条线现在是错误的,我用它来提取数组:self.leagueForm = arr.flatMap {Form($ 0)} – RDowns

+0

@RDowns将参数类型为'Form'的init方法更改为'[String:Any] ''而不是'[String:String]' –

+0

@RDowns'results_id'是Int,因此如果您将其声明为自定义类中的字符串,则将其转换为'Int'。 –

相关问题