gson parsing in Android Code

Gson is open source and a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.google.code.gson:gson:2.7'
    testImplementation 'junit:junit:4.12'
}

Make Sure below class used for how to call json file load to our Activity or fragment.

Utils.Java


import android.content.Context;
import java.io.InputStream;
/** * Created by Raj kumar on 21/08/16. */public class Utils {

    public static String loadJSONFromAsset(Context context) {
        String json = null;
        try {

            InputStream is = context.getAssets().open("Yourfilename.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }
}

MainActivity.java

In this class Just call loadJSONFromAsset(Context contextmethod ,set to your adapter class .
Like this:

    RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.rec_conent);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    YourAdapter adapter = new YourAdapter (this);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setAdapter(adapter);
    adapter.setOnClickListener(this);
    try {
        JSONObject obj = new JSONObject(Utils.loadJSONFromAsset(this));
        JSONArray jsonarray2 = obj.getJSONArray("array_name");
        if (jsonarray2.length() > 0) {

            adapter.setItems(jsonarray2);
            adapter.notifyDataSetChanged();
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
} 

Here i provide sample mysongs.json file.

{
  "MySongs": [
    {
      "url": "https://firebasestorage.googleapis.com/v0/b/firebase-pmusic.appspot.com/o/music%2F_Baitikochi%20Chuste%20-%20SenSongsMp3.Co.mp3?alt=media&token=ad2ec01a-33b1-4a3f-8e0d-08573dbad200",
      "name": "Baitikochi Chuste",
      "singername": "Anirudh Ravichander",
      "poster": "https://firebasestorage.googleapis.com/v0/b/firebase-pmusic.appspot.com/o/Images%2Fsong1.jpg?alt=media&token=3ffa05a1-6a07-4bad-9b9b-91681dbf2533"    },
    {
      "url": "https://firebasestorage.googleapis.com/v0/b/firebase-pmusic.appspot.com/o/music%2FAB%20Yevaro%20Nee%20Baby%20-%20SenSongsMp3.Co.mp3?alt=media&token=a5c3f485-038f-4ab3-a0ea-26f9e8943096",
      "name": "AB Yevaro Nee Baby",
      "singername": "Anirudh Ravichander",
      "poster": "https://firebasestorage.googleapis.com/v0/b/firebase-pmusic.appspot.com/o/Images%2Fsong2.jpg?alt=media&token=d9c528e1-2e17-4ef4-a3dd-b951f7e40ada"    },
    {
      "url": "https://firebasestorage.googleapis.com/v0/b/firebase-pmusic.appspot.com/o/music%2FGaali%20Vaaluga%20-%20SenSongsMp3.Co.mp3?alt=media&token=64aa331c-6e70-4acf-96db-0fd0180519fc",
      "name": "Gaali Vaaluga",
      "singername": "Anirudh Ravichander",
      "poster": "https://firebasestorage.googleapis.com/v0/b/firebase-pmusic.appspot.com/o/Images%2Fsong3.jpg?alt=media&token=bae2b497-1bf2-4635-950b-86e270925c0b"    }]}


Comments