How to parse local JSON file in assets?

后端 未结 4 1626
灰色年华
灰色年华 2021-02-14 20:25

I have a JSON file in my assets folder. That file has one object with an array. The array has 150+ objects with each having three strings.

For each of these 150+ objects

4条回答
  •  故里飘歌
    2021-02-14 20:49

    first you need to read the json file from assets , you can use this method

    public String readFile(String fileName) throws IOException
    {
      BufferedReader reader = null;
      reader = new BufferedReader(new InputStreamReader(getAssets().open(fileName), "UTF-8")); 
    
      String content = "";
      String line;
      while ((line = reader.readLine()) != null) 
      {
         content = content + line
      }
    
      return content;
    
    }
    

    then

    String jsonFileContent = readFile("json_in_assets.json");
    JSONArray jsonArray = new JSONArray(jsonFileContent);
    List persons = new ArrayList<>();
    for (int i=0;i

提交回复
热议问题