-1

I'm trying to parse JSON file to show in table view in Swift 3 without using external frameworks, but my knowledge in Swift is not enough. I have looked other questions, but nothing worked for me. The problem is that in my JSON there are couple of nested arrays. Here is example of the JSON file:

"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"
                    },

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

How I get parse the data into Swift arrays and use it to be displayed in UITableView controller. There are couple of "guides" in this JSON and I need to be able to show only one of them at the time. Help will be much appreciated. Thank you in advance.

2
  • 2
    There are countless existing examples of parsing JSON data. Please update your question with relevant code showing what you actually tried and clearly explain what issues you are having. Commented Dec 2, 2016 at 18:25
  • Possible duplicate of parsing json into an array in swift 3 Commented Dec 2, 2016 at 18:49

1 Answer 1

0

Try using following code:

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)")
        }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.