Android JSON Data Parsing Tutorial with Example
https://www.viralandroid.com/2016/12/android-json-data-parsing-tutorial-with-example.html
JSON stand for JavaScript Object Notation. It is a minimal, readable, format for structuring data. JSON is an independent data transmit between a server and application and is the best alternative for XML. It represents data in a text format so that can be easily parsed. In this android JSON data parsing tutorial you will learn to parse the response in order to get the required information in android app.
Android provide different classes to manipulate JSON data, they are JSONArray, JSONStringer, JSONObject and JSONTokenizer. An JSON file have many components like Array([), Objects({), Key and Value.
Related:
Android Push Notification Tutorial Using Firebase
YouTube Android API Integration: Getting Started with YouTube Android API
Turn ON and OFF WiFi Connection Programmatically in Android
Let’s create new android project called Android JSON Data Parsing Example to parse JSON data from web server to android app.
Following is the simple JSON data that we parse in android app. This JSON gives list of contact information.
This JSON data can be found at: https://api.myjson.com/bins/31b8v
We are getting JSON data from server, so we need to add internet permission in our project AndroidManifest.xml file. Just open AndroidManifest.xml file and add android.permission.INTERNET permission. AndroidManifest.xml file will looks like below.
AndroidManifest.xml
Open your activity_main.xml file and add ListView with an id to display JSON data in list view. Following is the content of activity_main.xml file.
res/activity_main.xml
Again create a new XML layout file list_item.xml and add three TextView vertically with id, textColor, textStyle, textSize, etc. Following is the complete content of list_item.xml file.
res/list_item.xml
Create a new java class HttpHandler.java and add the following java code.
src/HttpHandler.java
Open your MainActivity.java file and replace it with following code.
src/MainActivity.java
Now run your application, which looks like above screenshot. After running your app, you will see name, email address, and phone number in ListView. You can change these information according to your requirement. Remember to turn on the Internet connection on your device either WiFi or mobile data, because we have loading JSON data from internet.
Android provide different classes to manipulate JSON data, they are JSONArray, JSONStringer, JSONObject and JSONTokenizer. An JSON file have many components like Array([), Objects({), Key and Value.
Related:
Android Push Notification Tutorial Using Firebase
YouTube Android API Integration: Getting Started with YouTube Android API
Turn ON and OFF WiFi Connection Programmatically in Android
JSON Parsing Android Tutorial with Example
Let’s create new android project called Android JSON Data Parsing Example to parse JSON data from web server to android app.
Following is the simple JSON data that we parse in android app. This JSON gives list of contact information.
This JSON data can be found at: https://api.myjson.com/bins/31b8v
Adding Internet Permission
We are getting JSON data from server, so we need to add internet permission in our project AndroidManifest.xml file. Just open AndroidManifest.xml file and add android.permission.INTERNET permission. AndroidManifest.xml file will looks like below.
AndroidManifest.xml
XML Layout File
Open your activity_main.xml file and add ListView with an id to display JSON data in list view. Following is the content of activity_main.xml file.
res/activity_main.xml
Again create a new XML layout file list_item.xml and add three TextView vertically with id, textColor, textStyle, textSize, etc. Following is the complete content of list_item.xml file.
res/list_item.xml
Http Handler Class
Create a new java class HttpHandler.java and add the following java code.
src/HttpHandler.java
Modify MainActivity.java File
Open your MainActivity.java file and replace it with following code.
src/MainActivity.java
Now run your application, which looks like above screenshot. After running your app, you will see name, email address, and phone number in ListView. You can change these information according to your requirement. Remember to turn on the Internet connection on your device either WiFi or mobile data, because we have loading JSON data from internet.