2016-12-02 88 views
-1

我試圖解析JSON文件以在Swift 3中的表格視圖中顯示而不使用外部框架,但是我在Swift中的知識還不夠。我看了其他問題,但沒有爲我工作。問題是在我的JSON中有幾個嵌套數組。 這裏是例如JSON文件:解析Swift 3中的JSON數組

"guides" : [ 
    { 
     "name" : "First Guide", 
     "sections" : [ 
      { 
       "title" : "Controls", 
       "data" : [ 
        { 
         "value" : "controls.PNG", 
         "type" : "image" 
        }, 
        { 
         "value" : "Sensitivity: Changes the sensitivity of the camera when turning.", 
         "type" : "text" 
        }, 
        { 
         "value" : "Invert Y-Axis: Toggles inversion of camera when looking up/down.", 
         "type" : "text" 
        }, 
        { 
         "value" : "crosshair.PNG", 
         "type" : "image" 
        }, 
        { 
         "value" : "Lefty : Toggles the D-pad being on the left/right side of the screen.", 
         "type" : "text" 
        }, 
        { 
         "value" : "Swap Jump and Sneak : Chooses whether to swap the position of jump and sneak buttons.", 
         "type" : "text" 
        }, 
        { 
         "value" : "Button size - Changes the size of the buttons. Smaller buttons allow extra slots for the hotbar.", 
         "type" : "text" 
        } 
       ] 
      }, 
      { 
       "title" : "User Profile", 
       "data" : [ 
        { 
         "value" : "profile.png", 
         "type" : "image" 
        }, 
        { 
         "value" : "Use Cellular Data: Gives the Player the option to use cellular data.", 
         "type" : "text" 
        } 
       ] 
      }, 
      { 
       "title" : "Global Resources", 
       "data" : [ 
        { 
         "value" : "resources.png", 
         "type" : "image" 
        }, 

..............

我如何獲取數據解析成斯威夫特陣列和用它來顯示UITableView控制器。在這個JSON中有幾個「指南」,我需要能夠在當時只顯示其中的一個。 幫助將不勝感激。先謝謝你。

+2

有解析JSON數據的無數現有的例子。請用相關的代碼更新您的問題,以顯示您實際嘗試的內容,並清楚地解釋您遇到的問題。 – rmaddy

+0

[在swift 3中將json解析爲數組]可能的副本(http://stackoverflow.com/questions/39759998/parsing-json-into-an-array-in-swift-3) – Hasya

回答

0

嘗試使用以下代碼:

let jsonString = "[\"a\",\"b\"]" 
     let jsonData = jsonString.data(using: .utf8)! as Data 
     do { 
      let dataJson = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) 
      print(dataJson) 
     } 
     catch { 
      print("error getting xml string: \(error)") 
     }