iOSNewbie iOSNewbie - 4 months ago 7
JSON Question

How to convert JSON to Object and save it to Array

i got JSON in format like that:

[
{
"person": {
"name": "Dani",
"age": "24"
}
},
{
"person": {
"name": "ray",
"age": "70"
}
}
]


I want to create object of that and storage it in array, how can i do it? I use Alamofire and SwiftyJSON.

Answer

Using SwiftyJSON, you can convert JSON data to an array like following:

Suppose feedData is the JSON, you want to convert:

    do {
           let jsonDataToParse = try NSJSONSerialization.dataWithJSONObject(feedData, options: NSJSONWritingOptions.PrettyPrinted)
           let jsonData = JSON(data: jsonDataToParse)
           if let DataArray = jsonData.array{
                // use your array
           }
       }catch let error as NSError{
           print(error.description)
       } 
Comments