110

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?

yydl
  • 24,284
  • 16
  • 65
  • 104
  • 2
    Note - now that this question is 10 years old, the top answers are no good. Be sure to scroll down to the correct answers. It's just one line of code. – Fattie Mar 28 '21 at 19:08
  • how do I read the contents of a source XML file? – Phlip Jan 02 '22 at 19:54

8 Answers8

162

See openRawResource. Something like this should work:

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
  • 36,028
  • 10
  • 80
  • 93
  • 1
    What if i want to put the string in a String resource in android and use it dynamically using getResources().getString(R.String.name) ? – Ankur Gautam Jan 28 '14 at 17:47
  • For me it doesn´t work because of the quotes, which are ignored when reading and which also seem to can not be escaped – Marian Klühspies Dec 19 '14 at 12:18
  • 1
    Is there any way to make [ButterKnife](http://jakewharton.github.io/butterknife/) bind the raw resource? Writing 10+ lines of code just to read a string seems like a little overkill. – Jezor Aug 11 '16 at 02:00
  • 1
    How is the json stored inside the resources? Simply inside `\res\json_file.json` folder or inside `\res\raw\json_file.json`? – Cliff Burton May 11 '17 at 09:19
  • 2
    This answer lacks critical info. Where can `getResources()` be called? Where should the raw resource file go? What convention should you follow to ensure the build tools create `R.raw.json_file`? – NobodyMan Aug 08 '18 at 06:42
147

Kotlin is now official language for Android, so I think this would be useful for someone

val text = resources.openRawResource(R.raw.your_text_file)
                                 .bufferedReader().use { it.readText() }
Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
26

I used @kabuko's answer to create an object that loads from a JSON file, using Gson, from the Resources:

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);
    }
}

To use it, you'd do something like the following (the example is in an 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);
   }
jwir3
  • 6,019
  • 5
  • 47
  • 92
19

Like @mah states, the Android documentation (https://developer.android.com/guide/topics/resources/providing-resources.html) says that json files may be saved in the /raw directory under the /res (resources) directory in your project, for example:

MyProject/
  src/ 
    MyActivity.java
  res/
    drawable/ 
        graphic.png
    layout/ 
        main.xml
        info.xml
    mipmap/ 
        icon.png
    values/ 
        strings.xml
    raw/
        myjsonfile.json

Inside an Activity, the json file can be accessed through the R (Resources) class, and read to a String:

Context context = this;
Inputstream inputStream = context.getResources().openRawResource(R.raw.myjsonfile);
String jsonString = new Scanner(inputStream).useDelimiter("\\A").next();

This uses the Java class Scanner, leading to less lines of code than some other methods of reading a simple text / json file. The delimiter pattern \A means 'the beginning of the input'. .next() reads the next token, which is the whole file in this case.

There are multiple ways to parse the resulting json string:

ʕ ᵔᴥᵔ ʔ
  • 772
  • 1
  • 8
  • 14
14

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

raw/
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
mah
  • 39,056
  • 9
  • 76
  • 93
  • 5
    If I want to embed a JSON file in my app, where should I put it? in assets folder or raw folder? Thanks! – Ricardo Jan 17 '15 at 22:57
8

Found this Kotlin snippet answer very helpful ♥️

While the original question asked to get a JSON String, I figure some might find this useful. A step further with Gson leads to this little function with reified type:

private inline fun <reified T> readRawJson(@RawRes rawResId: Int): T {
    resources.openRawResource(rawResId).bufferedReader().use {
        return gson.fromJson<T>(it, object: TypeToken<T>() {}.type)
    }
}

Note you want to use TypeToken not just T::class so if you read a List<YourType> you won't lose the type by type erasure.

With the type inference you can then use like this:

fun pricingData(): List<PricingData> = readRawJson(R.raw.mock_pricing_data)
themightyjon
  • 1,241
  • 11
  • 12
5
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");
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
5

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() : "";
}
Mahdi Astanei
  • 1,905
  • 1
  • 17
  • 30