{"id":554,"date":"2017-07-10T05:09:53","date_gmt":"2017-07-10T05:09:53","guid":{"rendered":"http:\/\/abhiandroid.com\/programming\/?page_id=554"},"modified":"2019-06-14T11:45:58","modified_gmt":"2019-06-14T11:45:58","slug":"json","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/programming\/json","title":{"rendered":"JSON Parsing Tutorial With Example In Android Studio [Step by Step]"},"content":{"rendered":"<p>JSON stands for JavaScript Object Notation. It is structured, light weight, human readable and easy to parse. It&#8217;s a best alternative to XML when our android app needs to interchange data from server. XML parsing is very complex as compare to JSON parsing.<\/p>\n<p>JSON is shorter, quicker and easier way to interchange data from server. JSON is great success and most of the API available support JSON format.<\/p>\n<p>Android Provide us four different classes to manipulate JSON data. These classes are JSONObject, JSONArray, JSONStringer and JSONTokenizer.<\/p>\n<hr \/>\n<h4><strong>Sample JSON format:<\/strong><\/h4>\n<p>Below is the sample code of JSON. Its very simple JSON code which gives us the list of users where each object contain the information like user id, name, email, gender and different contact numbers.<\/p>\n<pre> {\r\n    \"users\": [\r\n        {\r\n                \"id\": \"1087\",\r\n                \"name\": \"Abhishek Saini\",\r\n                \"email\": \"info@abhiandroid.com\",\r\n                \"gender\" : \"male\",\r\n                \"contact\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        {\r\n                \"id\": \"1088\",\r\n                \"name\": \"Gourav\",\r\n                \"email\": \"gourav9188@gmail.com\",\r\n                \"gender\" : \"male\",\r\n                \"contact\": {\r\n                    \"mobile\": \"+91 0000000000\",\r\n                    \"home\": \"00 000000\",\r\n                    \"office\": \"00 000000\"\r\n                }\r\n        },\r\n        .\r\n        .\r\n        .\r\n        .\r\n  ]\r\n}<\/pre>\n<h4><strong>JSON Elements In Android:<\/strong><\/h4>\n<p>In Android, JSON consist of many components. Below we define some common components.<\/p>\n<p><strong><span style=\"color: #008000;\">1. Array([):\u00a0<\/span><\/strong> In a JSON, square bracket ([) represents a JSONArray. JSONArray values may be any mix of JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or NULL. Values may not be NaNs, infinities, or of any type not listed here.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. Objects({):<\/strong><\/span> In a JSON, curly bracket ({) represents a JSONObject. A JSONObject represents the data in the form of key and value pair. JSONObject values may be any mix of other JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or NULL. Values may not be NaNs, infinities, or of any type not listed here.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. key:\u00a0 <\/strong><\/span> A JSONObject contains a key that is in string format. A pair of key and value creates a JSONObject.<\/p>\n<p><strong><span style=\"color: #008000;\">4. Value:\u00a0<\/span><\/strong>Each key has a value that could be primitive datatype(integer, float, String etc).<\/p>\n<hr \/>\n<h4><strong>JSON Parsing In Android:<\/strong><\/h4>\n<p>Usually, JSON contain two types of nodes JSONArray and JSONObject so while parsing we have to use the appropriate method. If JSON starts from square bracket ([) we use getJSONArray() method and if it start from curly bracket ({) then we should use the getJSONObject() method. Apart from these there are some other methods for better parsing JSON data.<\/p>\n<hr \/>\n<h4><strong>JSONObjet Parsing methods:<\/strong><\/h4>\n<p>Below we define some important methods of JSONObject parsing which are mainly used for parsing the data from JSONObject.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. get(String name):<\/strong><\/span> This method is used to get the value from JSONObject. It returns the value of object type. We pass the String type key and it returns the value of Object type if exists otherwise it throws JSONException.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. getBoolean(String name):\u00a0<\/strong><\/span> This method is used to get the Boolean value from JSONObject. We pass the String type key and it returns the value of Boolean type if exists otherwise it throws JSONException.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. getDouble(String name):\u00a0<\/strong><\/span> This method is used to get the double type value from JSONObject. We pass the String type key and it returns the value in double type if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">4. getInt(String name):\u00a0<\/span><\/strong> This method is used to get the int type value from JSONObject. We pass the string type key and it returns the value in int type if exists otherwise it throws JSONException.<\/p>\n<p><span style=\"color: #008000;\"><strong>5. getJSONArray(String name):<\/strong><\/span> This method is used to get the JSONArray type value. We pass the String type key and it returns JSONArray if exists otherwise it throws JSONException.<\/p>\n<p><span style=\"color: #008000;\"><strong>6. getJSONObject(String name):<\/strong><\/span> This method is used to get the JSONObject type value. We pass the String type key and it returns the JSONObject value if exists otherwise it throws JSONException.<\/p>\n<p><span style=\"color: #008000;\"><strong>7. getLong(String name):<\/strong><\/span> This method is used to get the long type value from JSONObject. We pass the String type key and it returns the value in long type if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">8. getString(String name):<\/span><\/strong> This method is used to get the String type value from JSONObject. We pass the String type key and it returns the value in String type if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">9. length():<\/span><\/strong> This method is used to get the number of name\/value mappings in this object.<\/p>\n<p><strong><span style=\"color: #008000;\">10. keys():<\/span><\/strong> This method is used to get the iterator of String names in the Object.<\/p>\n<p><strong><span style=\"color: #008000;\">11. opt(String name):<\/span><\/strong> This method is used to get the value from JSONObject. It returns the value of Object type. We pass the String type key and it returns the value of Object type if exists otherwise it returns null.<\/p>\n<p><span style=\"color: #008000;\"><strong>12. optBoolean(String name):<\/strong><\/span> This method is used to get the Boolean value from JSONObject. We pass the String type key and it returns the value of Boolean type if exists otherwise it returns false.<\/p>\n<p><span style=\"color: #008000;\"><strong>13. optDouble(String name):<\/strong><\/span> This method is used to get the double type value from JSONObject. We pass the String type key and it returns the value in double type if exists otherwise it returns NaN.<\/p>\n<p><span style=\"color: #008000;\"><strong>14. optInt(String name):<\/strong><\/span> This method is used to get the int type value from JSONObject. We pass the string type key and it returns the value in int type if exists otherwise it returns 0.<\/p>\n<p><span style=\"color: #008000;\"><strong>15. optJSONArray(String name):<\/strong><\/span> This method is used to get the JSONArray type value from JSONObject. We pass the String type key and it returns JSONArray if exists otherwise it returns null.<\/p>\n<p><strong><span style=\"color: #008000;\">16. optJSONObject(String name):<\/span><\/strong> This method is used to get the other JSONObject type value from JSONObject. We pass the String type key and it returns the JSONObject value if exists otherwise it returns null.<\/p>\n<p><strong><span style=\"color: #008000;\">17. optLong(String name):<\/span><\/strong> This method is used to get the long type value from JSONObject. We pass the String type key and it returns the value in long type if exists otherwise it returns 0.<\/p>\n<p><strong><span style=\"color: #008000;\">18. optString(String name):<\/span><\/strong> This method is used to get the String type value from JSONObject. We pass the String type key and it returns the value in String type if exists otherwise it returns emptly(&#8220;&#8221;) string.<\/p>\n<hr \/>\n<h4><strong>JSONArray Parsing methods:<\/strong><\/h4>\n<p>Below we define some important methods of JSONArray parsing which are mainly used for parsing the data from JSONArray.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. get(int index):<\/strong><\/span> This method is used to get the value from JSONArray. It returns the value of object type. We pass the index and it returns the value of object type if exist otherwise it throws JSONException.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. getBoolean(int index):<\/strong><\/span> This method is used to get the Boolean value from JSONArray. We pass the index and it returns the value of Boolean type if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">3. getDouble(int index):<\/span><\/strong> This method is used to get the double type value from JSONArray. We pass the index and it returns the value in double type if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">4. getInt(int index):<\/span> <\/strong>This method is used to get the int type value from JSONArray. We pass the index and it returns the value in int type if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">5. getJSONArray(int index):<\/span><\/strong> This method is used to get the JSONArray type value. We pass the index and it returns JSONArray if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">6. getJSONObject(int index):<\/span><\/strong> This method is used to get the JSONObject type value. We pass the index and it returns the JSONObject value if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">7. getLong(int index):<\/span> <\/strong>This method is used to get the long type value from JSONArray. We pass the index and it returns the value in long type if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">8. getString(int index):<\/span><\/strong> This method is used to get the String type value from JSONArray. We pass the index and it returns the value in String type if exists otherwise it throws JSONException.<\/p>\n<p><strong><span style=\"color: #008000;\">9. length():<\/span><\/strong> This method is used to get the number of values in this Array.<\/p>\n<p><strong><span style=\"color: #008000;\">10. opt(int index):<\/span><\/strong> This method is used to get the value from JSONArray. It returns the value of Object type. We pass the index and it returns the value at index of Object type if exists otherwise it returns null.<\/p>\n<p><strong><span style=\"color: #008000;\">11. optBoolean(int index):<\/span><\/strong> This method is used to get the Boolean value from JSONArray. We pass the index and it returns the value of Boolean type if exists otherwise it returns false.<\/p>\n<p><strong><span style=\"color: #008000;\">12. optDouble(int index):<\/span> <\/strong>This method is used to get the double type value from JSONArray. We pass the index and it returns the value in double type if exists otherwise it returns NaN.<\/p>\n<p><strong><span style=\"color: #008000;\">13. optInt(int index):<\/span><\/strong> This method is used to get the int type value from JSONArray. We pass the index and it returns the value in int type if exists otherwise it returns 0.<\/p>\n<p><strong><span style=\"color: #008000;\">14. optJSONArray(int index):<\/span> <\/strong>This method is used to get the other JSONArray type value from JSONArray. We pass the index and it returns JSONArray if exists otherwise it returns null.<\/p>\n<p><strong><span style=\"color: #008000;\">15. optJSONObject(int index):<\/span><\/strong> This method is used to get the JSONObject type value from JSONArray. We pass the index and it returns the JSONObject value if exists otherwise it returns null.<\/p>\n<p><strong><span style=\"color: #008000;\">16. optLong(int index):<\/span><\/strong> This method is used to get the long type value from JSONArray. We pass the index and it returns the value in long type if exists otherwise it returns 0.<\/p>\n<p><strong><span style=\"color: #008000;\">17. optString(int index):<\/span><\/strong> This method is used to get the String type value from JSONArray. We pass the index and it returns the value in String type if exists otherwise it returns empty(&#8220;&#8221;) string.<\/p>\n<hr \/>\n<h4><strong>Example 1 of\u00a0<\/strong>Simple JSON Parsing In Android Studio:<\/h4>\n<p>Below is the example of JSON parsing in Android, In this example we parse the data from JSON and then display it in the UI.In this, we have employee name and salary stored in JSON format. Firstly we create two TextView&#8217;s in our XML file and then in our Activity we parse the data using JSONObject methods and set it in the TextView&#8217;s.<\/p>\n<p>Below you can download code, see final output and step by step explanation of the example:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"http:\/\/www.mediafire.com\/file\/97453qc5fj9qqha\/JSONParsingExample.zip\/file\" target=\"_blank\" rel=\"nofollow noopener\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-557\" src=\"\/programming\/wp-content\/uploads\/2017\/06\/JSON-Parsing-Example-in-Android-Studio.jpg\" alt=\"JSON Parsing Example in Android Studio\" width=\"259\" height=\"460\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2017\/06\/JSON-Parsing-Example-in-Android-Studio.jpg 259w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2017\/06\/JSON-Parsing-Example-in-Android-Studio-169x300.jpg 169w\" sizes=\"auto, (max-width: 259px) 100vw, 259px\" \/><\/p>\n<p><strong><span style=\"color: #008000;\">Step 1:\u00a0<\/span><\/strong> Create a new project and name it JSONParsingExample.<\/p>\n<p><strong><span style=\"color: #008000;\">Step 2:<\/span> <\/strong>Open res -&gt; layout -&gt; activity_main.xml (or) main.xml and add following code:<\/p>\n<p>In this step we create two TextView&#8217;s for displaying employee name and salary.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\"abhiandroid.com.jsonparsingexample.MainActivity\"&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/name\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"50dp\"\r\n        android:text=\"Name\"\r\n        android:textColor=\"#000\"\r\n        android:textSize=\"20sp\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/salary\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"80dp\"\r\n        android:text=\"Salary\"\r\n        android:textColor=\"#000\"\r\n        android:textSize=\"20sp\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Step 3 :<\/span><\/strong> Now open app -&gt; java -&gt; package -&gt; MainActivity.java and add the below code.<\/p>\n<p>In this step firstly we get the reference of both TextView&#8217;s then we parse the JSON using JSONObject methods and finally we set the data in Textview&#8217;s.<\/p>\n<pre>package abhiandroid.com.jsonparsingexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.widget.TextView;\r\n\r\nimport org.json.JSONException;\r\nimport org.json.JSONObject;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n\r\n    String JSON_STRING = \"{\\\"employee\\\":{\\\"name\\\":\\\"Abhishek Saini\\\",\\\"salary\\\":65000}}\";\r\n    String name, salary;\r\n    TextView employeeName, employeeSalary;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ get the reference of TextView's\r\n        employeeName = (TextView) findViewById(R.id.name);\r\n        employeeSalary = (TextView) findViewById(R.id.salary);\r\n\r\n        try {\r\n            \/\/ get JSONObject from JSON file\r\n            JSONObject obj = new JSONObject(JSON_STRING);\r\n            \/\/ fetch JSONObject named employee\r\n            JSONObject employee = obj.getJSONObject(\"employee\");\r\n            \/\/ get employee name and salary\r\n            name = employee.getString(\"name\");\r\n            salary = employee.getString(\"salary\");\r\n            \/\/ set employee name and salary in TextView's\r\n            employeeName.setText(\"Name: \"+name);\r\n            employeeSalary.setText(\"Salary: \"+salary);\r\n\r\n        } catch (JSONException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<hr \/>\n<h4><strong>JSON Parsing File Example 2 In Android Studio:<\/strong><\/h4>\n<p>Below is the 2nd example of JSON parsing In Android Studio. In this example we create a JSON file and store it in assets folder of Android. In this JSON file we have list of users where each object contain the information like user id, name, email, gender and different contact numbers. In this we are using LinearLayoutManager with vertical orientation to display the array items. Firstly we declare a RecyclerView in our XML file and then get the reference of it in our Activity. After that we fetch the JSON file then parse the JSONArray data and finally set the Adapter to show the items in RecyclerView. Whenever a user clicks on an item the name of the Person is displayed on the screen with the help of Toast.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"http:\/\/www.mediafire.com\/file\/97453qc5fj9qqha\/JSONParsingExample.zip\/file\" target=\"_blank\" rel=\"nofollow noopener\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-558\" src=\"\/programming\/wp-content\/uploads\/2017\/06\/JSON-Parsing-File-Example-in-Android-Studio.jpg\" alt=\"JSON Parsing File Example in Android Studio\" width=\"259\" height=\"460\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2017\/06\/JSON-Parsing-File-Example-in-Android-Studio.jpg 259w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2017\/06\/JSON-Parsing-File-Example-in-Android-Studio-169x300.jpg 169w\" sizes=\"auto, (max-width: 259px) 100vw, 259px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it JSONParsingExample.<\/p>\n<p><strong><span style=\"color: #008000;\">Step 2:<\/span><\/strong> Open Gradle Scripts &gt; build.gradle and add RecyclerView and CardView Library dependency in it.<\/p>\n<pre>apply plugin: 'com.android.application'\r\n\r\nandroid {\r\n    compileSdkVersion 24\r\n    buildToolsVersion \"24.0.1\"\r\n\r\n    defaultConfig {\r\n        applicationId \"abhiandroid.com.jsonparsingexample\"\r\n        minSdkVersion 16\r\n        targetSdkVersion 24\r\n        versionCode 1\r\n        versionName \"1.0\"\r\n    }\r\n    buildTypes {\r\n        release {\r\n            minifyEnabled false\r\n            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'\r\n        }\r\n    }\r\n}\r\n\r\ndependencies {\r\n    compile fileTree(dir: 'libs', include: ['*.jar'])\r\n    testCompile 'junit:junit:4.12'\r\n    compile 'com.android.support:appcompat-v7:24.1.1'\r\n    compile \"com.android.support:recyclerview-v7:23.0.1\" \/\/ dependency file for RecyclerView\r\n    compile 'com.android.support:cardview-v7:23.0.1' \/\/ dependency file for CardView\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open res -&gt; layout -&gt; activity_main.xml (or) main.xml and add following code:<\/p>\n<p>In this step we create a RecyclerView in our XML file.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\"abhiandroid.com.jsonparsingexample.MainActivity\"&gt;\r\n\r\n    &lt;android.support.v7.widget.RecyclerView\r\n        android:id=\"@+id\/recyclerView\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Create a new XML file rowlayout.xml for item of RecyclerView and paste the following code in it.<\/p>\n<p>In this step we create a new xml file for item row in which we create a TextView to show the data.<\/p>\n<pre> &lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n\r\n&lt;android.support.v7.widget.CardView xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:card_view=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:id=\"@+id\/card_view\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_margin=\"5dp\"\r\n    android:layout_height=\"wrap_content\"&gt;\r\n\r\n    &lt;LinearLayout\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:orientation=\"vertical\"\r\n        android:padding=\"10dp\"&gt;\r\n        &lt;!--\r\n        items for a single row of RecyclerView\r\n        --&gt;\r\n        &lt;TextView\r\n            android:id=\"@+id\/name\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:text=\"Name\"\r\n            android:textColor=\"#000\"\r\n            android:textSize=\"20sp\" \/&gt;\r\n\r\n        &lt;TextView\r\n            android:id=\"@+id\/email\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:text=\"email@email.com\"\r\n            android:textColor=\"#000\"\r\n            android:textSize=\"15sp\" \/&gt;\r\n\r\n        &lt;TextView\r\n            android:id=\"@+id\/mobileNo\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:text=\"e9999999999\"\r\n            android:textColor=\"#000\"\r\n            android:textSize=\"15sp\" \/&gt;\r\n    &lt;\/LinearLayout&gt;\r\n&lt;\/android.support.v7.widget.CardView&gt;\r\n\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Step 5 :<\/span><\/strong> Now open app -&gt; java -&gt; package -&gt; MainActivity.java and add the below code.<\/p>\n<p>In this step firstly we get the reference of RecyclerView. After that we fetch the JSON file from assets and parse the JSON data using JSONArray and JSONObject methods and then set a LayoutManager and finally we set the Adapter to show the items in RecyclerView.<\/p>\n<pre>package abhiandroid.com.jsonparsingexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.support.v7.widget.LinearLayoutManager;\r\nimport android.support.v7.widget.RecyclerView;\r\nimport android.util.Log;\r\n\r\nimport org.json.JSONArray;\r\nimport org.json.JSONException;\r\nimport org.json.JSONObject;\r\n\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.util.ArrayList;\r\nimport java.util.Arrays;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    \/\/ ArrayList for person names, email Id's and mobile numbers\r\n    ArrayList&lt;String&gt; personNames = new ArrayList&lt;&gt;();\r\n    ArrayList&lt;String&gt; emailIds = new ArrayList&lt;&gt;();\r\n    ArrayList&lt;String&gt; mobileNumbers = new ArrayList&lt;&gt;();\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ get the reference of RecyclerView\r\n        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);\r\n        \/\/ set a LinearLayoutManager with default vertical orientation\r\n        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());\r\n        recyclerView.setLayoutManager(linearLayoutManager);\r\n\r\n        try {\r\n            \/\/ get JSONObject from JSON file\r\n            JSONObject obj = new JSONObject(loadJSONFromAsset());\r\n            \/\/ fetch JSONArray named users\r\n            JSONArray userArray = obj.getJSONArray(\"users\");\r\n            \/\/ implement for loop for getting users list data\r\n            for (int i = 0; i &lt; userArray.length(); i++) {\r\n                \/\/ create a JSONObject for fetching single user data\r\n                JSONObject userDetail = userArray.getJSONObject(i);\r\n                \/\/ fetch email and name and store it in arraylist\r\n                personNames.add(userDetail.getString(\"name\"));\r\n                emailIds.add(userDetail.getString(\"email\"));\r\n                \/\/ create a object for getting contact data from JSONObject\r\n                JSONObject contact = userDetail.getJSONObject(\"contact\");\r\n                \/\/ fetch mobile number and store it in arraylist\r\n                mobileNumbers.add(contact.getString(\"mobile\"));\r\n            }\r\n        } catch (JSONException e) {\r\n            e.printStackTrace();\r\n        }\r\n\r\n        \/\/  call the constructor of CustomAdapter to send the reference and data to Adapter\r\n        CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, personNames, emailIds, mobileNumbers);\r\n        recyclerView.setAdapter(customAdapter); \/\/ set the Adapter to RecyclerView\r\n    }\r\n\r\n    public String loadJSONFromAsset() {\r\n        String json = null;\r\n        try {\r\n            InputStream is = getAssets().open(\"users_list.json\");\r\n            int size = is.available();\r\n            byte[] buffer = new byte[size];\r\n            is.read(buffer);\r\n            is.close();\r\n            json = new String(buffer, \"UTF-8\");\r\n        } catch (IOException ex) {\r\n            ex.printStackTrace();\r\n            return null;\r\n        }\r\n        return json;\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 6:<\/strong><\/span> Create a new class CustomAdapter.java inside package and add the following code.<\/p>\n<p>In this step we create a CustomAdapter class and extends RecyclerView.Adapter class with ViewHolder in it. After that we implement the overrided methods and create a constructor for getting the data from Activity, In this custom Adapter two methods are more important first is onCreateViewHolder in which we inflate the layout item xml and pass it to View Holder and other is onBindViewHolder in which we set the data in the view\u2019s with the help of ViewHolder.<\/p>\n<pre>package abhiandroid.com.jsonparsingexample;\r\n\r\nimport android.content.Context;\r\nimport android.support.v7.widget.RecyclerView;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.TextView;\r\nimport android.widget.Toast;\r\n\r\nimport java.util.ArrayList;\r\n\r\n\r\npublic class CustomAdapter extends RecyclerView.Adapter&lt;CustomAdapter.MyViewHolder&gt; {\r\n\r\n    ArrayList&lt;String&gt; personNames;\r\n    ArrayList&lt;String&gt; emailIds;\r\n    ArrayList&lt;String&gt; mobileNumbers;\r\n    Context context;\r\n\r\n    public CustomAdapter(Context context, ArrayList&lt;String&gt; personNames, ArrayList&lt;String&gt; emailIds, ArrayList&lt;String&gt; mobileNumbers) {\r\n        this.context = context;\r\n        this.personNames = personNames;\r\n        this.emailIds = emailIds;\r\n        this.mobileNumbers = mobileNumbers;\r\n    }\r\n\r\n    @Override\r\n    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {\r\n        \/\/ infalte the item Layout\r\n        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);\r\n        MyViewHolder vh = new MyViewHolder(v); \/\/ pass the view to View Holder\r\n        return vh;\r\n    }\r\n\r\n    @Override\r\n    public void onBindViewHolder(MyViewHolder holder, final int position) {\r\n        \/\/ set the data in items\r\n        holder.name.setText(personNames.get(position));\r\n        holder.email.setText(emailIds.get(position));\r\n        holder.mobileNo.setText(mobileNumbers.get(position));\r\n        \/\/ implement setOnClickListener event on item view.\r\n        holder.itemView.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View view) {\r\n                \/\/ display a toast with person name on item click\r\n                Toast.makeText(context, personNames.get(position), Toast.LENGTH_SHORT).show();\r\n            }\r\n        });\r\n\r\n    }\r\n\r\n\r\n    @Override\r\n    public int getItemCount() {\r\n        return personNames.size();\r\n    }\r\n\r\n    public class MyViewHolder extends RecyclerView.ViewHolder {\r\n        TextView name, email, mobileNo;\/\/ init the item view's\r\n\r\n        public MyViewHolder(View itemView) {\r\n            super(itemView);\r\n\r\n            \/\/ get the reference of item view's\r\n            name = (TextView) itemView.findViewById(R.id.name);\r\n            email = (TextView) itemView.findViewById(R.id.email);\r\n            mobileNo = (TextView) itemView.findViewById(R.id.mobileNo);\r\n\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JSON stands for JavaScript Object Notation. It is structured, light weight, human readable and easy to parse. It&#8217;s a best alternative to XML when our android app needs to interchange data from server. XML parsing is very complex as compare to JSON parsing. JSON is shorter, quicker and easier way to interchange data from server. &hellip; <a href=\"https:\/\/abhiandroid.com\/programming\/json\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JSON Parsing Tutorial With Example In Android Studio [Step by Step]<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"home.php","meta":{"footnotes":""},"class_list":["post-554","page","type-page","status-publish","hentry"],"psp_head":"<title>JSON Parsing Tutorial With Example In Android Studio [Step by Step] \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn JSON parsing in Android Studio following our step by step tutorial with two examples. JSON stands for JavaScript Object Notation.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/programming\/json\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/pages\/554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/comments?post=554"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/pages\/554\/revisions"}],"predecessor-version":[{"id":992,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/pages\/554\/revisions\/992"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/media?parent=554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}