An Introduction to JSON in iOS 7

Introduction

This article explains how to work with JavaScript Object Notation (JSON) in iOS 7. The following dexcibes JSON:

  • It is a web service and easy to understand.
  • JSON is a very lightweight data interchange format. It is inherited from the NSObject class.
  • JSON is formatted text and easy to parse and integrate. It takes less time to load over the EXtensible Markup Language (XML).
  • In JSON it is easy to store and exchange the data. JSON is commonly used for structural data.
  • JSON is used for the interchange of the data in client/server applications.
  • Most iOS apps like Facebook send the data in JSON format.
  • In JSON we import the file “SBJson.h”.
  • In JSON we use NSArray or NSDictionary. NSArray represents [] square brackets. NSDictionary represents {} curly braces brackets.
JSON over XML

There are so many advantages to the use of JavaScript Object Notation (JSON) over the EXtensible Markup Language (XML).

  • In JSON it is easier to parse and generate the data than for XML.
  • In JSON it is easier to fetch the data than for XML.
  • In JSON it takes less time to load than for XML.
  • In JSON it is smaller in size than for XML.
  • JSON requires fewer tags XML.
  • In JSON values can be either Boolean or number but XML is only strings.

XML (Nested XML)

  • It is declared in the NSXMParsing.h file. It is Inherit from the NSObject class.
  • XML is written only with a string like <text>.
  • XML also processes and exchanges data between a client and server.
  • XML documents represent a hierarchical tree structure.

NSXml

  • It is a set of classes. In XML we can create, update and modify the query.
  • NSXML represents a logical tree structure.
  • DTD (document type definition) is also represented in NSXML.
Parsing and handling the XML

There are two type of APIs used to handle and parse XML.

  • Tree Based API
  • Event-Driven API

Tree Based API

  • The tree based API it is based on an internal tree structure that confirms only a logical structure.
  • It takes a lot of memory to make the internal tree.
Event-Driven API
  • In the event-driven API it deals with only one XML at a time.
  • It takes less memory to parse the data.
Method of XML (nested XML)

The following are some methods of nested XML:

  • -(NS Integer)ColumnNumber
  • -(NS Integer)lineNumber

In the first method of XML it will return the columnNumber of XML.

In the second method of XML it will return the lineNumber of XML.

Data Model of NSXML

The data model of NSXML represents various types of XML documents that are explained here.

  • Nodes
  • Attribute

In the data model of NSXML Node has the unique identity.

In the data model of NSXML Attribute has two types.

  • Index.
  • Level.

An Index indicates the Zero based number.

Level indicates numbers like one, two.

Method of JSON

The following are some methods of JSON:

  • connection didReceiveResponse
  • connection didReceiveData
  • connection didFailWithError
  • connectionDidFinishLoading

Connection didReceiveResponse

In JSON the method of Connection didReceiveResponse method is used at run time. It will be used in the beginning of sending the data.

Connection didReceiveData

In JSON the method of connection didReciveRespons method is used for only receiving only new data. The incoming data is appended by NSMutubale data.

Connection didFailWithError

In JSON the method of connection didFailWithError method is used to check the occurance of a connection error or not.

ConnectionDidFinishLoading

In JSON the method of connectionDidFinishLoading method is used only when the incoming data is loaded


Similar Articles