Today,  I will share how to parse JSON data in swift 3, which we are getting actually from the online data source.

It is one of the most important topics of iOS, as when we talk about any app for production phase without cloud-based data storage it does not make any sense.

If we will talk about any game, then we may require storing data on the cloud and restore data from the cloud server, when user signs in again, so as to restore the progress of the game. For the same reason, in this Cloud-based era, it is hard to think about any apps without any Server Side storage.

Pre-requisite knowledge:

  1. Use of Alamofire 4.0 (A robust networking library for the iOS)
  2. How to install and update Cocoapods.
  3. How to add HTTP App Transport security in your App.

Create a new project.

 

Create a UI Something to request the data and add the corresponding outlets. Below figure will guide you to create a new outlet.

basic-coding-screen-in-json-parsing-elements

 

How to create:-

Import the Alamofire Library by adding  “import Alamofire”. It appears just below the “import UIKit” at the very top corner of the viewController.swift file.

In the outlet of the Action Button (on which we are supposed to fetch data with press down action).


Alamofire.request(“URL”, method: .get, encoding: URLEncoding.default, headers:[ “Content-Type”: “application/x-www-form-urlencoded”, ])
.responseJSON { response in
switch response.result {
case.success(let JSON):
}
case.failure(let error):
}

Now this Alamofire function will request the mentioned server to get the response.  We will parse that response in the JSON data.

If we succeed in  response.result.data , the “success” block will trigger else failure block will trigger.

In the URL method, put down the URL and request some response from the server.

Here, I am using my own build API just for the testing point of view. You can choose any other API or use this API for the testing purpose.


Alamofire.request("http://vishwaraj.xyz/iOSServer/", method: .get, encoding: URLEncoding.default, headers:[ "Content-Type": "application/x-www-form-urlencoded", ])
.responseJSON { response in
switch response.result {
case.success(let JSON):
self.emails.remove(at: 0)
let response = JSON as! [[String: AnyObject]] for keys in response {
//Add the task
}

case.failure(let error):
print(“Error %@”, error.localizedDescription)
}

}

Run the existing code. If error occurs, it will appear something like this on console.

 

http-transport-security-error

App Transport Security (ATS) lets an app add a declaration to its Info.plist file. This file specifies the domains with which it needs secure communication. ATS prevents accidental disclosure, provides secure default behaviour, and is easy to adopt. Try to adopt ATS for creating a new app or updating an existing one.

If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, start using HTTPS as much as you can, and migrate the rest of your app as soon as possible.

For more information, please visit my blog.

Just to solve this issue, go through your “info.plist” file .

Right click on the info.plist and open Source Code. Add all the following code before the end of the program.


NSAppTransportSecurity

NSExceptionDomains

vishwaraj.xyz

NSIncludesSubdomains
NSExceptionAllowsInsecureHTTPLoads
NSExceptionRequiresForwardSecrecy
NSExceptionMinimumTLSVersion
TLSv1.2
NSThirdPartyExceptionAllowsInsecureHTTPLoads

NSThirdPartyExceptionRequiresForwardSecrecy
NSThirdPartyExceptionMinimumTLSVersion
TLSv1.2
NSRequiresCertificateTransparency


You will find exception domain added to this code which was earlier rejected by Xcode.

If you wish to make NSDictionary of responses, add the below-mentioned code in the case.success block.

let response = JSON as! [[String: AnyObject]]

Use the following code, to have specific values of NSDictionary, by appending in a blank array, we have declared in the class.


for keys in response {

let key = (keys as NSDictionary).object(forKey: “email”)!

self.emails.append(key as! String);

}




Now through this code we will get the list of emails which we are getting from my API.

You can download the whole source code for this json parsing code from my GitHub repository.

Load More By qwertyasvishwa
Load More In code corner

Check Also

Buil Your First Camera App – Swift

Today,  I will share how to make a Camera App, and store the clicked Selfie and upload the…