how to fetch data from local json file?

前端 未结 4 1249
自闭症患者
自闭症患者 2021-01-15 05:50

I created a json file locally. I can view that in data>data>com.example.storage>files>filename.json. Using this, I want to fetch all the text values locally & download a

4条回答
  •  忘掉有多难
    2021-01-15 06:39

    here is code read JSON data from file that file keep in asset folder in project

    public JSONObject readDummyResponse() {
        InputStream inputStream = null;
        try {
            inputStream = this.getAssets().open("sample_response.txt");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        JSONObject jsonObject = null;
        if (inputStream != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream));
            StringBuffer buffer = new StringBuffer();
            String statement;
            try {
                while ((statement = reader.readLine()) != null) {
                    if (statement.trim().length() > 0) {
                        buffer.append(statement);
                    }
                }
            } catch (IOException e1) { // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if (buffer.length() > 0) {
                try {
                    jsonObject = new JSONObject(buffer.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return (JSONObject) jsonObject;
    }
    

提交回复
热议问题