In iOS 5 apple introduced a new built -in API for communicating with the JSON.This API is really helpful to work with JSON read and write functionalities. If you are not familiar with JSON (JavaScript Object Notation), it is a lightweight data-interchange format that is often used to send data over a network connection.
It is easy for humans to read and write. It is easy for machines to parse and generate. It’s So simple and You can learn more about the JSON from here. Why JSON is Important, Nowadays, most of the powerful third party’s such as Google, Yahoo provides JSON API to interchange data with the server and client. When you make a web service call with a query string, it will return JSON formatted data.
When you write your own web services, it is easy to convert your data into JSON format, and also the learning part of JSON is quite simple. Most attractive part of JSON based web service is Intrinsic Simplicity. In this tutorial, we will learn about how we can work with JSON in IOS 5.0 and above.
In the previous versions of IOS (below version 5), to parse the JSON data we are using a third party library know usSBJson. It is a bit heavy process and we need to include all the files to our project, and also the data manipulation process is also bit hard. So finally apple added a JSON Parser in to the Cocoa library.
Top level object must be NSArray or NSDictionary . All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull. All dictionary keys are instances of NSString. All Numbers are not NaN or infinity. In this tutorial, we are going to get hands-on experience in the new native JSON support.
Step by step we are going to build a sample application which helps us to understand the working of JSON native Support. Open Xcode from the developer tools, Click on the Create new project and select the single view based application. Enter the Product name as JSON,enter the Company identifier name and change the device family to iPhone,click Next and save the project by clicking Create.Start up screen will look like below.
Next we need to add a text file to the project,for that we need to right click on the project click on the other category and click on the empty file template. Now we need to rename the empty file as jsonText.txt . Next we need to add the JSON data to the jsonText.txt. Now we need to create a method to read the local jsonText.txt data and parse the JSON to the Foundation objects.
Open the ViewController.m and create a function to interact with the parse JSON text button in the UI. The below code snippet will indicate the implementation of JSON Parser . UI button named parse JSON Text, when ou touch up inside the button, it will call the parseLocalJSON function and convert the jsonText file data to the NSData.
Next code snippet will explain about the conversion model of NSData to the NSDictionary. At the end of the fetchedData function it will call the processData function with the JSON object dictionary as parameter. This function will separate according to the key-value process. Now touch up inside the Parse JSON text button, it will change the UI according to the JSON Value. Just replace this code snippet with the above code. Getting the file path from the local bundle. You are done. You can download the source code from here.
If there are no contacts on your phone, the contents of your All Contacts group will be synced. 13c. If you have all your contacts on your device and want to import them into Google, you can synchronize your Google Contacts with iTunes. All Contacts and Calendars that you sync with iTunes will be put into the On My iPhone Contacts and Calendar groups on your phone.
0 Comments