2016-12-02 141 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)") 
     }