JSON Parsing

MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
  ArrayList < HashMap < String, String >> contactList;
  Button btnParseJson;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   contactList = new ArrayList < > ();
   btnParseJson = (Button) findViewById(R.id.idBtnParseJson);
   btnParseJson.setOnClickListener(new View.OnClickListener() {
    @Override 
     public void onClick(View view) {
     beginJsonParsing();
   }
  });
 }
  private void beginJsonParsing() {
   Toast.makeText(this, "11", Toast.LENGTH_SHORT).show();
  
  try {
    JSONObject reader = new JSONObject(loadJSONFromAsset());
    JSONArray jArray = reader.getJSONArray("rootnode");
   
   for (int i = 0; i < jArray.length(); i++) {
    
    try {
      JSONObject obj = jArray.getJSONObject(i);
      // Pulling items from the array 
      int id = obj.getInt("Id");
      String name = obj.getString("Name");
      String surname = obj.getString("Surname");
      int age = obj.getInt("Age");
      addingValuesToHasMap(id, name, surname, age);
     
    } catch (JSONException e) {
      // Oops 
      e.printStackTrace();
     
    }
    
   }
    ListView lv = (ListView) findViewById(R.id.idLvJson);
    ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, 
     R.layout.list_item, new String[] {
    "Id",
    "Name",
    "Surname",
    "Age"
   },  new int[] {
    R.id.idSNO, R.id.idName, R.id.idSurname, R.id.idAge
   });
    lv.setAdapter(adapter);
   
  } catch (JSONException e) {
    e.printStackTrace();
  }
 
 }
  private void addingValuesToHasMap(int id, String name, String surname, int age) {
   // tmp hash map for single contact 
   HashMap < String, String > contact = new HashMap < > ();
   // adding each child node to HashMap key => value 
   contact.put("Id", Integer.toString(id));
   contact.put("Name", name);
   contact.put("Surname", surname);
   contact.put("Age", Integer.toString(age));
   // adding contact to contact list
   contactList.add(contact);
  
 }
  public String loadJSONFromAsset() {
   String json = null;
  
  try {
    InputStream is = this.getAssets().open("parsingData/data.json");
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();
    json = new String(buffer, "UTF-8");
   
  } catch (IOException ex) {
    ex.printStackTrace();

   return null;
  }
  return json;
 }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="html.tutorial.com.parsing.MainActivity" 
     android:orientation="vertical"> 
    
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/idBtnParseJson" 
     android:text="Parse Json"  />
    
    <ListView 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/idLvJson"></ListView>
</LinearLayout>  ​

list_item.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal"
    android:layout_width="match_parent" 
    android:padding="16dp" 
    android:layout_height="match_parent">  
    
    <TextView 
     android:id="@+id/idSNO" 
     android:layout_width="32dp" 
     android:layout_height="wrap_content"
     android:text="1"
     android:textColor="@color/colorAccent" /> 
    
    <TextView 
     android:id="@+id/idName" 
     android:layout_weight="1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" 
     android:textColor="#5d5d5d" 
     android:text="Arjun" 
     android:textStyle="bold" />
    
    <TextView 
     android:id="@+id/idSurname" 
     android:layout_weight="1"
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"
     android:textColor="#5d5d5d" 
     android:text="Toshniwal" 
     android:textStyle="bold" /> 
    
    <TextView 
     android:id="@+id/idAge"
     android:layout_width="24dp" 
     android:layout_height="wrap_content"
     android:textColor="#5d5d5d" 
     android:text="28" 
     android:textStyle="bold" /> 
</LinearLayout>  

data.json
{
   "rootnode":[
      {
         "Id":1,
         "Name":"Ajay",
         "Surname":"Mehra",
         "Age":25
      },
      {
         "Id":2,
         "Name":"Mukesh",
         "Surname":"Sharma",
         "Age":24
      },
      {
         "Id":3,
         "Name":"Ankit",
         "Surname":"Tiwari",
         "Age":21
      },
      {
         "Id":4,
         "Name":"Sachin",
         "Surname":"Patel",
         "Age":26
      }
   ]
}

Share this