在Android应用程序资源中使用JSON文件

假设我的应用程序的原始资源文件夹中有JSON内容的文件。 我怎样才能读到这个应用程序,以便我可以parsingJSON?

请参阅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(); 

我用@ kabuko的答案来创build一个对象,使用Gson从JSON文件加载,从资源:

 package com.jingit.mobile.testsupport; import java.io.*; import android.content.res.Resources; import android.util.Log; import com.google.gson.Gson; import com.google.gson.GsonBuilder; /** * An object for reading from a JSON resource file and constructing an object from that resource file using Gson. */ public class JSONResourceReader { // === [ Private Data Members ] ============================================ // Our JSON, in string form. private String jsonString; private static final String LOGTAG = JSONResourceReader.class.getSimpleName(); // === [ Public API ] ====================================================== /** * Read from a resources file and create a {@link JSONResourceReader} object that will allow the creation of other * objects from this resource. * * @param resources An application {@link Resources} object. * @param id The id for the resource to load, typically held in the raw/ folder. */ public JSONResourceReader(Resources resources, int id) { InputStream resourceReader = resources.openRawResource(id); Writer writer = new StringWriter(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(resourceReader, "UTF-8")); String line = reader.readLine(); while (line != null) { writer.write(line); line = reader.readLine(); } } catch (Exception e) { Log.e(LOGTAG, "Unhandled exception while using JSONResourceReader", e); } finally { try { resourceReader.close(); } catch (Exception e) { Log.e(LOGTAG, "Unhandled exception while using JSONResourceReader", e); } } jsonString = writer.toString(); } /** * Build an object from the specified JSON resource using Gson. * * @param type The type of the object to build. * * @return An object of type T, with member fields populated using Gson. */ public <T> T constructUsingGson(Class<T> type) { Gson gson = new GsonBuilder().create(); return gson.fromJson(jsonString, type); } } 

要使用它,你需要做如下的事情(例子在InstrumentationTestCase ):

  @Override public void setUp() { // Load our JSON file. JSONResourceReader reader = new JSONResourceReader(getInstrumentation().getContext().getResources(), R.raw.jsonfile); MyJsonObject jsonObj = reader.constructUsingGson(MyJsonObject.class); } 

Kotlin现在是Android的官方语言,所以我认为这将是有用的人

 val text = resources.openRawResource(R.raw.your_text_file) .bufferedReader().use { it.readText() } 

来自http://developer.android.com/guide/topics/resources/providing-resources.html

生的/
任意文件以原始格式保存。 要使用原始InputStream打开这些资源,请使用资源ID(即R.raw.filename)调用Resources.openRawResource()。

但是,如果您需要访问原始文件名和文件层次结构,则可以考虑在assets /目录中保存一些资源(而不是res / raw /)。 assets /中的文件没有给定资源ID,所以只能使用AssetManager读取它们。

 InputStream is = mContext.getResources().openRawResource(R.raw.json_regions); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); String json = new String(buffer, "UTF-8");