2016-09-18 57 views
1

我試圖訪問[ 「上限類別」] [ 「第一類」] [0] [0]本地JSON文件的無法從JSON訪問嵌套的字典/陣列,迅速

{ 
    "upper category": { 
     "first category": [ 
      [ 
       "this is a question", 
       "this is an answer", 
       "this is the rank" 
      ], 
      [ 
       "this is a question2", 
       "this is an answer2", 
       "this is the rank2" 
      ] 
     ], 
     "second category": [ 
      [ 
       "this is a question3", 
       "this is an answer3", 
       "this is the rank3" 
      ] 
     ] 
    } 
} 

let path = Bundle.main.path(forResource: "data", ofType: "json") 
do {let data:NSData = try NSData(contentsOfFile: path!) 
    let json = try? JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject 

我無法訪問超出第一個字典的任何內容。

我試了幾次有類似的選項(我試過多箇舊的解決方案,但他們似乎並沒有爲我工作,也許迅疾3)

if let description = ((((json?["upper category"] as? AnyObject)?["first category"] as? AnyObject)?[0] as? AnyObject)?[0] as? String) { 

這可能是一個noob問題,我是新來的IOS。雖然任何答案非常讚賞,解釋如何編寫代碼,嵌套類型的其他數將是最好

(Xcode中8,快捷3)

回答

1

這是一個有點矯枉過正,但你可以投的JSON來[String:[String:[[String]]]]

let json = try? JSONSerialization.jsonObject(...) as? [String:[String:[[String]]]] 
+0

Thx它的作品,這是否被認爲是一個很好的做法? – temo