• Latest Code...

    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) {
            Toolbar toolbar = (Toolbar) findViewById(R.id.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();
        @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];
                json = new String(buffer, "UTF-8");
            } catch (IOException ex) {
                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 !!!

    1 comment:

    1. To remove the monitoring software from the phone may become a hard task too, look at how to hack someones phone on iphone here.



    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace