How To Parsing Json From Assets Using Gson In Android Tutorial

Please follow and like us:

json parsing from assets

GSON is open source and standalone library which is used to convert JSON data into java objects and vice versa. It provides two methods fromJson() -“ convert JSON format to java objects” and toJson() – “create JSON from java objects”. In previous tutorial we have seen about “Json Array” and “Json Object” parsing.In this tutorial we are going to discuss about JSON parsing for JSON Array formats using GSON Library from assets folder.

How To Parsing Json From Assets Using Gson In Android Tutorial

 

Json Parsing From Assets Example step by step

Parse JSON Array from assets using GSON
Create jsonname.json file and put it into assets folder.

JSON format:

[
   {
      "date":"11/8/2014",
      "auther":"nirav kalola",
      "description":"json object parsing using gson library is easy",
      "post_name":"json object parsing"
   },
   {
      "date":"12/8/2014",
      "auther":"nirav kalola",
      "description":"json array parsing using gson library",
      "post_name":"json array parsing"
   },
   {
      "date":"17/8/2014",
      "auther":"nirav kalola",
      "description":"store json file in assets folder and get data when required",
      "post_name":"json parsing from assets folder"
   }
]

In JSON, “[” stands for JSON Array and “{” stands for JSON Object.
We need to create one model class “BeanPost” and annotate field names with the SerializedName annotation. When we add serialized name to field must match with key name from JSON

import com.google.gson.annotations.SerializedName;
public class BeanPost {
    @SerializedName("post_name")
    private String post_name;
    @SerializedName("auther")
    private String auther;
    @SerializedName("date")
    private String date;
    @SerializedName("description")
    private String description;
    public BeanPost(String post_name, String auther, String date, String description) {
        this.post_name = post_name;
        this.auther = auther;
        this.date = date;
        this.description = description;
    }
    public String getPost_name() {
        return post_name;
    }
    public void setPost_name(String post_name) {
        this.post_name = post_name;
    }
    public String getAuther() {
        return auther;
    }
    public void setAuther(String auther) {
        this.auther = auther;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

Create loadJSONFromAsset method for retrieving json data

public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = getAssets().open("json.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;
    }

Create MainActivity.java Activity class and create GSON instance get data in required format.

new AsyncTask<Void,Void,Void>(){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressDialog=new ProgressDialog(MyActivity.this);
                progressDialog.setCancelable(false);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
            @Override
            protected Void doInBackground(Void... voids) {
                Type listType = new TypeToken<ArrayList<BeanPost>>(){}.getType();
                beanPostArrayList = new GsonBuilder().create().fromJson(loadJSONFromAsset(), listType);
                postList=new StringBuffer();
                for(BeanPost post: beanPostArrayList){
                    postList.append("\n title: "+post.getPost_name()+"\n auther: "+post.getAuther()+"\n date: "+post.getDate()+"\n description: "+post.getDescription()+"\n\n");
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                progressDialog.dismiss();
                txtPostList.setText(postList);
            }
        }.execute();

 

With GSON library, we can easily parse JSON format in our Android Application. Stay connected for latest android development tutorials for beginners.

nirav kalola