Understanding the concept of JSON


When you were a child you used to request chocolates from your dad and he used to buy it for you, really childhood memories are awesome.

In todays session we are going to look closely at JSON, which happens to be the preferred option that web developers are using as an output response.

Well consider your childhood memories for a second, your iPhone is the child who will request chocolates (i.e. data for a particular request) from the server who’s the dad here and the server (dad) will do the required processing for the requested chocolate (data) and will give the chocolate to the child (iPhone), this is what happens in all the iOS application that have server request involved.

The processing on the iPhone should be fast, your user should not wait long to see his requested data, so it all depends upon how quickly you can parse (read) the response given by the server and display it in the iPhone UI (user interface).

Keeping in mind that we are working with an environment where speed is the key, JSON provides a better solution in parsing the response returned by the server.

JSON (JavaScript Object Notation) it’s a response format, which is composed of key value pairs, you supply a key and get the value from it just like you do with NSDictionary class.

A JSON response may contain a single dictionary with array or a single array with multiple dictionaries or a single dictionary with some keys or all of them, the structure of the response depends upon what you have requested from the server, so you can say that JSON is a container of your requested data from the server but in key value format.

You request your query to the server with the help of NSURLConnection class which later provides you with NSData object from the server in it’s delegate method named “connection did finish”, this NSData object is your actual response which you have received for the request you made, but you cannot read this response since it’s in bytes.

In order to work with JSON we have a class called as NSJSONSerialization which helps us to convert the NSData object into actual readable JSON key-value pair type, earlier we used a third party library called as the SBJSON.

You might have a question here like how can we identify if the JSON format is a array or dictionary, well the answer is very simple if the JSON format contains a “{} “ curly bracket then its clear that it’s a dictionary and if the JSON response has a square bracket “[]” then it’s an array.

Few years ago i worked on a project which had a really nice and a complicated JSON structure which i would like to share with you, hope this will help you in the learning curve.

Image 1 : JSON is a container of your data which you get from the server.



Look at the above image the response format is in curly bracket and hence we can say that it's a dictionary type, and if the response was an array then it contained square bracket [].

Image 2: After expanding the JSON tag i can see that my response contains two set of arrays and one key named status.



Image 3: After expanding the result tag which is an array you can see that it contains a list of twenty dictionaries within itself at various index.


 Let's expand a dictionary from the result tag and see what it contains

Image 4 : The dictionary present at the 0th index of the result tag array again contains multiple objects of various types



By looking at the above structure we can conclude that a JSON is a container of our data which we request from the server and each item present in the container can have multiple items of their own in key-value pair.

I use a link to identify and study the JSON response form the server so that I can code accordingly hope this link will help you to Online JSON Reader.

I hope that after reading this post your concepts of JSON will be more clear and you can now identify the JSON structure and code as per your server response.

You may enter your queries or suggestions via mail or comments and i will revert back to you, until then Happy iCoding and have a great day.

Join Us on Facebook

Comments

  1. Very Very useful post for learning JSON...
    Thank you so much for writing these one for us.....
    Very clearly explained json and in very easy way...today got each and evry point clear for JSON...now waiting for the code part...

    ReplyDelete
  2. nice explanation Sir.....
    Thanks for it..

    ReplyDelete
  3. thank you so much ravi...i am waiting for next part.

    ReplyDelete
  4. please provide how to read web services data
    through JSON (explen programatically).

    ReplyDelete
  5. how to add json framework in our progam

    ReplyDelete
  6. @unknown : You may use a third party library called as SBJSON or if you are building an app is from iOS 5.1 onwards then there is a class called as NSJSONSerialization which you can use read about it at the below link :

    https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

    ReplyDelete

Post a Comment