Using JSON File in Android App Resources

前端 未结 8 1169
盖世英雄少女心
盖世英雄少女心 2020-11-28 21:00

Suppose I have a file with JSON contents in the raw resources folder in my app. How can I read this into the app, so that I can parse the JSON?

8条回答
  •  -上瘾入骨i
    2020-11-28 21:06

    Using:

    String json_string = readRawResource(R.raw.json)
    

    Functions:

    public String readRawResource(@RawRes int res) {
        return readStream(context.getResources().openRawResource(res));
    }
    
    private String readStream(InputStream is) {
        Scanner s = new Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
    

提交回复
热议问题