Simple JSON Parsing Example in Android – Part I

In this blog post, ill explain you what is JSON and how to create JSON data and display it in Android in Different ways. So lets gets started with About JSON?

My Other blog post on JSON Tutorial.

  1. Part I – What is JSON and how to code JSON data on Php
  2. Part II – How to retrieve a simplest JSON data in Android
  3. Part III – How to retrieve a JSON data to ListView in Android
  4. Part IV – How to retrieve a complex typed JSON data in Android
  5. Part V – How to retrieve a JSON data from a Secured Site with Basic Authentication
  6. Part VI – How to Pass a JSON data to Php as a Parameters

 

1. What is JSON

  • JSON stands for JavaScript Object Notation
  • JSON is lightweight text-data interchange format
  • JSON is language independent
  • JSON is “self-describing” and easy to understand
  • JSON is minimal, textual, and a subset of JavaScript.
  • JSON is an alternative to XML.

Advantage of JSON over XML

  • JSON is faster and easier than xml for AJAX applications.
  • Unlike XML, it is shorter and quicker to read and write.
  • It uses array.

Android provides support to parse the JSON Object and Array. Well What is JSON Objects and Arrays??

JSON Object

A JSON object contains key/value pairs like map. The keys are strings and the values are the JSON types. Keys and values are separated by comma. The { (curly brace) represents the JSON Object.


{

"mobileOS": {

"OS":       "Android",

"currentVersion":      "5.0",

"VersionName":     "LolliPop"

}

}

JSON Array

The [ (square bracket) represents the JSON Array.


["Eclair", "Froyo","Gingerbread", "HoneyComb","Ice-cream Sandwich", "JellyBean", "KitKat", "LolliPop"]

Overall JSON Node

This is very simple JSON example which gives us list of Android OS Version where each node contains version information like Version Name, Version No and API Level.

{
"Android_Version_List": [
{
"Version Name": "Eclair",
"Version No": "2.0 - 2.1",
"API Level": "5 - 7",
},
{
"Version Name": "Froyo",
"Version No": "2.2 - 2.2.3",
"API Level": "8",
},

{
"Version Name": "GingerBread",
"Version No": "2.3 - 2.3.7",
"API Level": "9 - 10",
},
{
"Version Name": "HoneyComb",
"Version No": "3.0 - 3.2.6",
"API Level": "11 - 13",
},
{
"Version Name": "ICS",
"Version No": "4.0 - 4.0.4",
"API Level": "14 - 15",
},
{
"Version Name": "JellyBean",
"Version No": "4.1 - 4.3.1",
"API Level": "16 - 18",
},
{
"Version Name": "KitKat",
"Version No": "4.4 - 4.4.4",
"API Level": "19",
},
{
"Version Name": "Lollipop",
"Version No": "5.0",
"API Level": "21",
}
]
}

If your JSON node starts with [, then we should use getJSONArray() method. Same as if the node starts with {, then we should use getJSONObject() method.

 

Creating JSON data in PHP

Here we going to know about the JSON library in php. Two funcitons : json_encode() and json_decode() are very useful converting and parsing JSON data through PHP.

Simple Example of creating JSON data in PHP :

Example 1: Just a Text.


<?php
$data = array('OS' => 'Android', 'versionName' => 'LolliPop', 'currentVersion' => 'Android 5.0');
echo json_encode($data);
?>

Example 2: A list of single Items.


<?php
$data = array('Android_OS' =>
array( "Eclair",
"Froyo",
"Gingerbread",
"HoneyComb",
"Ice-cream Sandwich",
"JellyBean",
"KitKat",
"LolliPop"));
print (json_encode($data));
?>

Example 3: A list of Multiple Items.

<?php
$encode = array("Android Version List" =>
 array(array("Version Name" => "Eclair",
 "Version No" => "2.0 - 2.1",
 "API Level" => "5 - 7"),
 array("Version Name"=> "Froyo",
 "Version No"=> "2.2 - 2.2.3",
 "API Level"=> "8"),
 array("Version Name"=> "GingerBread",
 "Version No"=> "2.3 - 2.3.7",
 "API Level"=> "9 - 10"),
 array("Version Name"=> "HoneyComb",
 "Version No"=> "3.0 - 3.2.6",
 "API Level"=> "11 - 13"),
 array("Version Name"=> "ICS",
 "Version No"=> "4.0 - 4.0.4",
 "API Level"=> "14 - 15"),
 array("Version Name"=> "JellyBean",
 "Version No"=> "4.1 - 4.3.1",
 "API Level"=> "16 - 18"),
 array("Version Name"=> "KitKat",
 "Version No"=> "4.4 - 4.4.4",
 "API Level"=> "19"),
 array("Version Name"=> "Lollipop",
 "Version No"=> "5.0",
 "API Level"=> "21")));

$json_encode = json_encode($encode);
echo $json_encode."<br/>";
?>

Example 4:Complex typed Json data.

<?php
$encode = array('Owner Info'=>
            array('Name'=>'Durga Chiranjeevi','DOB'=>'xxxx', 'Gender'=>'Male'),
 
           'Website'=>'www.skholingua.com',
 
           'Content'=>array(array('content' => 'Android Application Development', 'webpage' => 'yes'),
                           array('content' => 'Core Java', 'webpage' => 'yes'),
                           array('content' => 'SQLite', 'webpage' => 'yes'),
                           array('content' => 'OpenGL ES', 'webpage' => 'yes'))
 
 
);
            
$json_encode = json_encode($encode);
echo $json_encode."<br/>";
?>

Example 5: Php accept Json data as a parameters.

<?php
 
  // Decode JSON-formatted parameter named "params"
  $json = $_POST["params"];
  $params = json_decode($json);
 
  // Echo individual parameters
  echo "Parsed from JSON:\r\n";
  if($params >0)
  foreach($params as $key => $value) {
    echo "$key = $value\r\n";
  }
 
?>

Now save these files in .php extensions and upload it to your server.

Now to Retrieve it in Android, Please read my other blog post which are linked to this post.
My Other blog post on JSON Tutorial.

  1. Part I – What is JSON and how to code JSON data on Php
  2. Part II – How to retrieve a simplest JSON data in Android
  3. Part III – How to retrieve a JSON data to ListView in Android
  4. Part IV – How to retrieve a complex typed JSON data in Android
  5. Part V – How to retrieve a JSON data from a Secured Site with Basic Authentication
  6. Part VI – How to Pass a JSON data to Php as a Parameters

 

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi
Comments
  1. Gokul

Leave a Reply

Your email address will not be published. Required fields are marked *