假设我的应用程序的原始资源文件夹中有一个包含JSON内容的文件。如何将其读入应用程序,以便解析JSON?
参见openRawResource。这样的事情应该起作用:
InputStream is = getResources().openRawResource(R.raw.json_file); Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } String jsonString = writer.toString();