Load json file into android studio

Asked 2 years ago, Updated 2 years ago, 50 views

It's a simple question. If you have a json file, is it possible to print out json's information in the listView of the android studio?

android json

2022-09-22 15:29

1 Answers

Yes, it's possible. You can put the json file in the assets folder and use the getAssets().open() method to read the json file, convert it to string, and convert it to JSONObject.

It will be a simple code like below.

public String loadJSONFromAsset() {
        String json = null;
        try {

            InputStream is = getAsset().open("file name.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;

    }


JSONObject obj = new JSONObject(json_return_by_the_function); 
//Pars JSONObject and draw it on the list as you want.


2022-09-22 15:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.