• Latest Code...

    Featured Post

    Implementing Hilt in a Kotlin Android Jetpack Compose Project with MVVM Architecture

     In modern Android development, maintaining a scalable codebase can be challenging, especially when it comes to dependency management. Hilt,...

    Load or get json from assets and parse in android studio

    Basically, when developing app there is data getting from web server, however , here json getting from assets, which is sometimes helpful when static data used in application.

    First you have create Assets folder in main.
    Right click on app, then select New and go to Folder and click finally you can see assets folder and click, like this



    then put json file in assets folder in your app.
    Now, in MainActivity,

    public class MainActivity extends AppCompatActivity {
    
        @Override    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override            public void onClick(View view) {
    //                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)//                        .setAction("Action", null).show();                String jsonString=loadJSONFromAsset();
                    Toast.makeText(getApplicationContext(),""+jsonString,Toast.LENGTH_SHORT).show();
    
                }
            });
        }
    
        @Override    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        public String loadJSONFromAsset() {
            String json = null;
            try {
    
                InputStream is = getAssets().open("emp.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;
    
        }
    
        @Override    public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    
    and Click FloatAction button you will see json in String.
    
    
    After you got json as a string, then you have parse as your required.
    first you have to convert string in jsonobject.

    JSONObject  jsonObject = new JSONObject(jsonString);

    In last, you have to parse jsonOject as your preset tag in jsonString, here you have to remember that it will be necessary exactly same inside the "".

    JSONObject emp_obj = jsonObject .getJSONObject("employees");

    Happy Coding !!!

    Contact Form

    Name

    Email *

    Message *