0

I currently have code that will take the json data I give it and parse it however I have everything wrapped in a for loop and I don't know how to take it out.

JSON data:

[
{
"id": 1,
"displayName": "Jacob Blacksten",
"department": "DF",
"mamager": "San",
"office": "NYC",
"util": 2
}
]

Working code:

do {
   let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers)

      guard let array = json as? [Any] else {return}

      for user in array {
          guard let userDict = user as? [String: Any] else {return}
          guard let id = userDict["id"] as? Int else { return }
          guard let name = userDict["displayName"] as? String else { return}
          guard let department = userDict["department"] as? String else {return}
          guard let manager = userDict["mamager"] as? String else {return}
          guard let office = userDict["office"] as? String else {return}
          guard let util = userDict["util"] as? Int else {return}

                print(id)
                print(name)
                print(department)
                print(manager)
                print(office)
                print(util)
            }

        } catch{
           print(error)
        }

You will notice everything is inside a for loop creating an array of all "users". However I only have one user and I want to be able to pull out say "id" and use it as a variable to later print it out in a label in my app. I hope this makes sense. I basically just want to manipulate this JSON data so that way I can use it and print them in labels in my app.

1
  • Why do you assign a type with mutableContainers to an immutable constant (let json)? Commented Jul 7, 2017 at 19:08

1 Answer 1

1

If you're trying to use your variables elsewhere, you just need to save them to something outside of the scope of your loop. I would suggest creating a User class and saving the values from the json to an instance of User.

// User.swift
class User {
    // Identifier
    let id: Int

    // Instance variables
    var name: String?
    var department: String?
    var manager: String?
    var office: String?
    var util: Int?

    init(withId id: Int) {
        self.id = id
    }
}

Then, you can just change your loop to be something like this.

var user: User?

do {
    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers)

    guard let array = json as? [Any],
      let userDict = array[0] as? [String: Any],
      let id = userDict["id"] as? Int,
      let name = userDict["displayName"] as? String,
      let department = userDict["department"] as? String,
      let manager = userDict["manager"] as? String,
      let office = userDict["office"] as? String,
      let util = userDict["util"] as? Int else { 
        return
    }

    user = User(withId: id)

    user.name = name
    user.department = department
    user.manager = manager
    user.office = office
    user.util = util

} catch{
    print(error)
}

 // Here, the user variable has all the updated info
 userIdLabel?.text = "\(user?.id)"

Note: Did this all in browser, didn't test

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.