Android Json Parsing and Image Loading Tutorial - Android Studio
In this tutorial we are going to learn how to parse a very complex json and how to load and display images in a ListView but before that we will first learn what is json. This is how our final result will look like:
What is JSON?
- JSON stands for JavaScript Object Notation, a subset of JavaScript
- JSON is a very lightweight data-interchange format
- JSON is language independent
- JSON is structured, easy to parse for machines and easy to read for humans
- JSON is better and a good alternative to XML
JSON has two parts:
- JSON Objects
- JSON Arrays
JSON Objects:
- Represented by Curly brackets
- Can contain JSONArrays, JSONObjects or Key-Value Pairs inside
JSON Arrays:
- Represented by Square brackets
- Only contains JSON objects inside
Key Value Pairs:
- Key and value are separated by colons ( : ) and a pair of key-value is separated by comma ( , )
As a rule of thumb, whenever you see a curly bracket in JSON you know for sure that its a JSONObject and when you see a square bracket, its a JSONArray.
It you have a node with curly bracket ( { ) you can use the getJsonObject() to get its value and if the node is starting with square bracket ( [ ) you can use the getJsonArray() method to get the array of JSONObjects.
It you have a node with curly bracket ( { ) you can use the getJsonObject() to get its value and if the node is starting with square bracket ( [ ) you can use the getJsonArray() method to get the array of JSONObjects.
Sample JSON
Following is the JSON we will parse in our tutorial. You can see the complete json by clicking on the following link: http://jsonparsing.parseapp.com/jsonData/moviesData.txt
{
"movies": [
{
"movie": "Avengers",
"year": 2012,
"rating": 7.8,
"duration": "141 min",
"director": "Joss Whedon",
"tagline": "A new age begins",
"cast": [
{
"name": "Robert Downey Jr." },
{
"name": "Chris Evans" },
{
"name": "Mark Ruffalo" }
],
"image": "http://jsonparsing.parseapp.com/jsonData/images/avengers.jpg",
"story": "When Tony Stark and Bruce Banner try to jump-start a dormant peacekeeping program called Ultron, things go horribly wrong and it's up to Earth's Mightiest Heroes to stop the villainous Ultron from enacting his terrible plans."
},
.....
.....Download the complete project from this GitHub link: https://github.com/hishamMuneer/JsonParsingDemo}


Comments
Post a Comment