amjad amjad - 7 months ago 30
Swift Question

load from json array into object array swift

I have array of user

var User:[User] = []


I want by alamofire and swiftyjson get info from http://jsonplaceholder.typicode.com/users
I know how to request and then i have array of json

how i can make loop through json array and create object of user then append in array
User[]


I think my problem with loop , this is from swifty json page

for (index,subJson):(String, JSON) in json {
//Do something you want
}


how i can use it in my App?

Answer

You can use similar following code

for (_, subDataJSON): (String, JSON) in dataJSON {
  let u = User()
  u.name = subDataJSON["name"].stringValue
  u.id = subDataJSON["id"].intValue
  u.active = subDataJSON["active"].boolValue
  self.User.append(u)
}

Parse your data json in loop then at the end of loop append single user object to global class User array like that.

Then use your class scoped User array like as you want.