2

I have this subJSON["guestpics"] as JSON data from SwiftyJSON.

When I print(subJSON["guestpics"]) I have this:

[
  "/images\/profile_pic\/1.jpg",
  "/images\/profile_pic\/2.jpg",
  "/images\/profile_pic\/3.jpg"
]

How can I convert this to an array ?

 for (_, subJSON): (String, JSON) in json[0]["data"] {
     print(subJSON["guestpics"])
 }
2
  • 1
    So, what is the type of subJSON["guestpics"], it seems like it is an array of strings. Commented Mar 28, 2016 at 18:22
  • [JSON] ? it comes from let json = JSON(jsonData) Commented Mar 28, 2016 at 18:24

2 Answers 2

1

SwiftyJSON has already parsed your JSON data and prepared typed objects.

If the key subJSON["guestpics"] contains an array, then use SwiftyJSON's optional getter .array to get it:

if let guestPicsArray = subJSON["guestpics"].array {
    // here "guestPicsArray" is your array
}
Sign up to request clarification or add additional context in comments.

2 Comments

Eric is correct. The JSON is already parsed when you call the JSON (data: ...) method. Therefore when you query for the guestpics key doing subJSON["guestpics"] you already have an array of strings. So you can do something like Eric suggested or let array = subJSON["guestpics"].array to store the array in a variable.
By the way, how can i loop this guestPicsArray ?
0

Why not directly storing the value of the key in the array? as it look like array
Does it show any error/crash when you are trying to store ?

if let arrGuest = subJSON["guestpics"] as? Array<String> {

}

Or if you are more familiar with Objective-c

if let arrGuest = arr as? NSArray {

}

you can get the array in the arrGuest object

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.