Thursday 19 December 2013

JSON Android Tutorial

JSON(JavaScript Object Notation)
Before going to JSON Details, First need to observe about Serialization. So you can easily understand about JSON.
So what is Serialization?
 It is the process of Converting the data into the stream of bytes into stream of information which other System can Understand. Primary purpose of java serialization is to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again. When there are two different parties involved, you need a protocol to rebuild the exact same object again. Java serialization API just provides you that. Other ways you can leverage the feature of serialization is, you can use it to perform a deep copy.

Why I used ‘primary purpose’ in the above definition is, sometimes people use java serialization as a replacement for database. Just a placeholder where you can persist an object across sessions. This is not the primary purpose of java serialization. Sometimes, when I interview candidates for Java I hear them saying java serialization is used for storing (to preserve the state) an object and retrieving it. They use it synonymously with database. This is a wrong perception for serialization.
Different Types of Serialization:
1. NameValuePair
2. XML
3. JSON
4. SOAP Protocol
5. Serializable Classes
Name Value Pair:
              A simple class encapsulating an attribute/value pair.It is used to send small amount of data to the destination.
XML(Extensible Mark-Up Language):
         XML is an Extensible Markup Language, a metalanguage that allows users to define their own customized markup languages.

Defination of JSON:
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language.
The advantage of JSON is, it is more light weight than other and parsing is more faster other than that.

JSON is built on two structures:
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.




Example:
Object Type JSON:
   {
    'name' : 'Tara',
    'phone' : 123456789
   };

Array Type JSON:
  [
      {
         ‘name' : 'Tara',
         'phone' : 123456789
      },
      {
         ‘name' : 'Tara',
         'phone' : 123456789
      },
        .
        .
        .
    ];

In Next tutorial, we will see how JSON works in Android and how to parse json in Android.

No comments:

Post a Comment