Skip to main content

How to parse a json file In Android

                   In this blog I am going to explain How to parse a json file. Here I am going to explain how to parse a json file stored in Asset folder. Here my file name is user_details.json which stored in asset folder(json_file/user_details.json). Here is my json file.

user_details.json

{
  "Name": "Shidhin TS",
  "UserId" : "1",
  "Website" : "http://shidhints.com",
  "Company" : "B-Mates Technologies",
  "Mobile" : "+91XXXXXXXXXX",
  "Qualification": [
    {
            "Name": "Name1",
            "Institute": "Institute Name1",
    "Year": "2006 - 2009",
            "Mark": "87%"
    }, 
    {
            "Name": "Name2",
            "Institute": "Institute Name2",
    "Year": "2004 - 2006",
            "Mark": "78%"
    }, 
    {
             "Name": "Name3",
            "Institute": "Institute Name3",
    "Year": "2004",
            "Mark": "93%"
    }
  ]
}

                  In this Json file I am having Strings Name, UserId, Website, Company, Mobile and an Array Qualification which is also contains some Strings.

Java Code

                   Here we are parsing the json file with the help of JSONObject. First we will create a JSONObject and we will extract the String using .getString() method. Here is the sample code.
   
       try {
InputStream is = getApplicationContext().getAssets().open("json_file/user_details.json");
String jString = ReadInputStreamAsString(is);
JSONObject jObject = new JSONObject(jString);

//Extracting Strings
String nameStr = jObject.getString("Name");  // Extract Name
String userIdStr = jObject.getString("UserId");  // Extract UserId
String websiteStr = jObject.getString("Website");  // Extract Website
String companyStr = jObject.getString("Company");  // Extract Website
String mobileStr = jObject.getString("Mobile");   // Extract Mobile


//Extracting Qualification Array
   JSONArray viewsObject = jObject.getJSONArray("Qualification");
for (int i = 0; i < viewsObject.length(); i++) {
JSONObject object = viewsObject.getJSONObject(i); //Creating Json object
String name = object.getString("Name");
String institute = object.getString("Institute");
String year = object.getString("Year");
String mark = object.getString("Mark");
}

} catch (Exception e) {
Helper.reportException(CreateCard.this, e);
}


ReadInputStreamAsString

                               This Function will convert the given InputStream to a String. Using this string we will create a json Object to parse the json.

         public static String ReadInputStreamAsString(InputStream in) throws IOException {

BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while (result != -1) {
byte b = (byte) result;
buf.write(b);
result = bis.read();
}
return buf.toString();
        }


Popular posts from this blog

Importing Image From Camera

In this tutorial I want to explain how to take a picture in camera ans import that picture to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_con…

How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.

In this blog I want to explain How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.


Intent
                Intent is an object that is used to launch an activity or to return information  from an activity.  To start a new activity using startActivity(intent).  To get information from another activity we start activity using startActivityForResult(intent, requestCode).


               Here in  FromPage.java we are adding  Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List values to an intent. In ToPage.java We extract added values will retrieve from intent. 


FromPage.java

ArrayList<Integer>  IntArrayList = new  ArrayList<Integer>(Arrays.asList(100,13,18,22,44,13));
ArrayList<String>  StringArrayList = new  ArrayList<String>(Arrays.asList("String1", "String2", "String3", "String4"));

        int[] intList = {4, 6, 3, …

Importing Image From Gallery

In this tutorial I want to explain how to import images from Gallery to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will redirect to Gallery to Browse image. While we select an image it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        …