What is the best way to parse JSON format in iOS Swift?

I have a project that checks a url that returns JSON format values. Any recommendations on how best to parse the result for my iOS app?

+5
source share
2 answers

You can also just make out

var data = NSData(contentsOfURL: NSURL(string: "http://api.androidhive.info/contacts/")!) var parcedData : NSMutableDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments, error: nil) as! NSMutableDictionary print(parceData) 

There are many other ways to do this.

You can use Alamofire with SwiftyJSON

Snippet with Alamofire and SwiftyJSON

 Alamofire.request(.GET, url, parameters: parameters) .responseJSON { (req, res, json, error) in if(error != nil) { NSLog("Error: \(error)") println(req) println(res) } else { NSLog("Success: \(url)") var json = JSON(json!) } } 
+1
source

First of all, there is no such thing as a better way. If there was a better way, you would probably hear it or find it among the best google hits.

You can do-it-yourself with NSJSONSerialization . This is what Apple provides, and it is simply the fastest and most difficult to use. It's not even that complicated, it just gets complicated when JSON has manly sub-levels.

I can recommend you SwiftyJSON . This was a minor (barely noticeable in most applications) overhead, but much easier in Swift. A great example can be found on the raywenderlich website.

+2
source

All Articles