Android Json Parsing basics tutorial

Step by Step guide to learn Json and parsing it in android.  

JSON is very light weight, structured, easy to parse and much human readable. JSON is best alternative to XML when your android app needs to interchange data with your server. 

In this tutorial we are going to learn how to parse JSON in android.But Before that we need to get the basics of Json correctly understood so that it could become easy to implement.

JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML.
Json_Basic_tutorial androidgreeve

You can see the difference between an XML and Json in the below code.

Simple Example :

Json Version of It.

    {"firstName":"John", "lastName":"Doe"}, 
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}

This XML syntax also defines an employees object with 3 employee records:
The XML version of it.

        <firstName>John</firstName> <lastName>Doe</lastName>
        <firstName>Anna</firstName> <lastName>Smith</lastName>
        <firstName>Peter</firstName> <lastName>Jones</lastName>

Android provides four different classes to manipulate JSON data. 

These classes are JSONArray,JSONObject,JSONStringer and JSONTokenizer.

The following table will help to understand in detail about it.

JSON Syntax Rules

JSON syntax is a subset of the JavaScript object notation syntax:
  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays
JSON data is written as name/value pairs.

A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:


JSON Values

JSON values can be:
  • A number (integer or floating point)
  • A string (in double quotes)
  • A Boolean (true or false)
  • An array (in square brackets)
  • An object (in curly braces)
  • null

Lets see some Sample JsonArray String 

JSONArray String is like this

"internalName": "blaaa",
"dataVersion": 0,
"name": "Domin91",
"profileIconId": 578,
"revisionId": 0,
"internalName": "blooo",
"dataVersion": 0,
"name": "Domin91",
"profileIconId": 578,
"revisionId": 0,

An example of JSONObject Is like this
"internalName": "domin91",
"dataVersion": 0,
"name": "Domin91",
"profileIconId": 578,
"revisionId": 0,

You can find the difference in the braces of the JSonArray and JsonObject.

Why Json?Why not XML? Difference?Advantages  all this question answer can be found below.

Advantages of JSON
  • Smaller message size
  • More structural information in the document
    • Can easily distinguish between the number 1 and the string "1" as numbers, strings (and Booleans) are represented differently in JSON.
    • Can easily distinguish between single items and collections of size one (using JSON arrays).
  • Easier to represent a null value
  • Easily consumed by JavaScript
Advantages of XML
  • Namespaces allow for sharing of standard structures
  • Better representation for inheritance
  • Standard ways of expressing the structure of the document: XML schema, DTD, etc
  • Parsing standards: DOM, SAX, StAX
  • Standards for querying: XQuery and XPath
  • Standards for transforming a document: XSLT
  • Human Readable
  • Easy to parse
You can find lots of tools to convert from XMl to Json and viceversa.

Here we end the basics of Json.In the next tutorial we will how to actually parse json in Android app , and it advantages.

If any doubts feel free to ask in the comment below.Any suggestion to be added you are welcomed.

Alll the world is a laboratory to the inquiring mind! Join Us


Suzan said…
Thanks for starting this series! Json is what i wanted to learn from a long time and your blog comes handy to it
Raj said…
Nice to start from the basics thanks!!!!
Arya Febiyan said…
Nice Tutorial. Thank You. I Am Waiting For Next Lesson.
Nagaraju P said…
Thanks for sharing basic knowledge on JSON Parsing........:)
Anonymous said…
Thank you for a valuable and easy-to-understand tutorial! If anybody is interested, I can share some good info about pecularities of applications development - view here.

Popular posts from this blog

The Guide to DeepLearning with Tensorflow and Keras - The Beginning

Android Navigation Drawer sliding menu with fragments

Conditional Statements in Python - A Comprehensive Beginners Guide