using swift 1.2, xcode 6.3 , ios 8, im trying build object json response using nsjsonserialization class. json response is:
[{ "_id" : "5470def9e0c0be27780121d7", "imageurl" : "https:\/\/s3-eu-west-1.amazonaws.com\/myapi-static\/clubs\/5470def9e0c0be27780121d7_180.png", "name" : "mondo", "hasvip" : false, "location" : { "city" : "madrid" } }, { "_id" : "540b2ff281b30f3504a1c72f", "imageurl" : "https:\/\/s3-eu-west-1.amazonaws.com\/myapi-static\/clubs\/540b2ff281b30f3504a1c72f_180.png", "name" : "teatro kapital", "hasvip" : false, "location" : { "address" : "atocha, 125", "city" : "madrid" } }, { "_id" : "540cd44581b30f3504a1c73b", "imageurl" : "https:\/\/s3-eu-west-1.amazonaws.com\/myapi-static\/clubs\/540cd44581b30f3504a1c73b_180.png", "name" : "charada", "hasvip" : false, "location" : { "address" : "la bola, 13", "city" : "madrid" } }] the object class (club.swift) nsjsonserialization.jsonobjectwithdata implementation is:
class club: nsobject { var id: string = "" var name: string = "" var imageurl: string = "" var hasvip: bool = false var desc: string = "" var location: [location] = [] init(jsonstring: string) { super.init() var error : nserror? let jsondata = jsonstring.datausingencoding(nsutf8stringencoding, allowlossyconversion: false) let jsondictionary: nsdictionary = nsjsonserialization.jsonobjectwithdata(jsondata!, options: nil, error: &error) as! nsdictionary self.setvaluesforkeyswithdictionary(jsondictionary [nsobject : anyobject]) } } and apiclient class is
class apiclient { func getlist(completionhandler: ([json]) -> ()) { let url = nsurl(string: "https://myapi.com/v1/clubs") let mutableurlrequest = nsmutableurlrequest(url: url!) mutableurlrequest.setvalue("content-type", forhttpheaderfield: "application/json") mutableurlrequest.httpmethod = "get" mutableurlrequest.setvalue("bearer r01.insg3xjv/r1ldkhkgoanpv53xqufdkpm0en5lidxx875fbjduzln1jtulkvjqvjsnwde1oqu2wuzjpaybiwwhw==", forhttpheaderfield: "authorization") let manager = alamofire.manager.sharedinstance let request = manager.request(mutableurlrequest) request.responsejson { (request, response, json , error) in if (json != nil){ var jsonobj = json(json!) if let data = jsonobj["hits"].arrayvalue [json]?{ var aclub : club = club(jsonstring: data) println(aclub.name) completionhandler(data) } } } } } but problem when try println(aclub.name) error
"cannot invoke initializer type'club' argument list of type (jsonstring [json])"
i dont know, how use nsjsonserialization class complex json response.
the jsonobj appear swiftyjson object, or that, 1 uses in lieu of nsjsonserialization, not in conjunction it. data variable array of json objects (i.e. it's [json]), not string.
but you're using alamofire's responsejson method, json parsing you. don't need use either nsjsonserialization or swiftyjson. it's parsed array of dictionaries.
if want array of club objects, can iterate through array, building club objects dictionaries:
class apiclient { func getlist(completionhandler: ([club]?, nserror?) -> ()) { let url = nsurl(string: "https://myapi.com/v1/clubs") let mutableurlrequest = nsmutableurlrequest(url: url!) mutableurlrequest.setvalue("content-type", forhttpheaderfield: "application/json") mutableurlrequest.httpmethod = "get" mutableurlrequest.setvalue("bearer r01.insg3xjv/r1ldkhkgoanpv53xqufdkpm0en5lidxx875fbjduzln1jtulkvjqvjsnwde1oqu2wuzjpaybiwwhw==", forhttpheaderfield: "authorization") let manager = alamofire.manager.sharedinstance let request = manager.request(mutableurlrequest) request.responsejson { (request, response, json, error) in var clubs = [club]() if let arrayofdictionaries = json as? [[string: anyobject]] { dictionary in arrayofdictionaries { clubs.append(club(dictionary: dictionary)) } completionhandler(clubs, nil) } else { completionhandler(nil, error) } } } } you have change club handle dictionary object:
class club { var id: string! var name: string! var imageurl: string! var hasvippler: bool! var location: [string: string]! init(dictionary: [string: anyobject]) { id = dictionary["_id"] as? string name = dictionary["name"] as? string imageurl = dictionary["imageurl"] as? string hasvippler = dictionary["hasvip"] as? bool location = dictionary["location"] as? [string: string] } } finally, table view controller call api:
let apiclient = apiclient() var clubs: [club]! override func viewdidload() { super.viewdidload() apiclient.getlist() { clubs, error in if clubs != nil { self.clubs = clubs self.tableview.reloaddata() } else { println(error) } } }