SoFunction
Updated on 2025-04-09

How to access assets local json file in Android

Purpose: When fixed json data is used in the App, such as: national city list, class members, etc., the json data can be made into local json files and stored in the assets folder.

The steps are as follows:

1. Create a new assets folder and copy the json file to this folder

Create a new assets file in the main folder, and then create a new folder in the assets file to copy the json data into the file.

2. Access json file

try {
      AssetManager assetManager = getAssets(); // Get assets explorer (the files in assets cannot be accessed directly, and can be accessed using AssetManager)      InputStreamReader inputStreamReader = new InputStreamReader((""),"UTF-8"); // Use IO stream to read json file content      BufferedReader br = new BufferedReader(inputStreamReader);
      String line;
      StringBuilder builder = new StringBuilder();
      while ((line = ())!=null){
        (line);
      }
      ();
      ();

      JSONObject testJson = new JSONObject(()); // Read data in json from builder.      // Pass directly into JSONObject to construct an instance      JSONArray array = ("nameList");

      ("nameList",());

      for (int i = 0;i<();i++){
        strings[i] = (i);
      }


    } catch (IOException e) {
      ();
    } catch (JSONException e) {
      ();
    }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.