90

I have the following json file. I want to know where should i place the json file in my project and how to read and store it.

{
"aakash": [
    [  0.070020,0.400684],
    [  0.134198,0.515837],
    [  0.393489,0.731809],
    [  0.281616,0.739490]

],
"anuj": [
    [  1287.836667,-22.104523],
    [  -22.104523,308.689613],
    [  775.712801,-13.047385],
    [  -13.047385,200.067743]
]
}
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47
  • if you have static json file then you can place it inside assets and raw folders in your Project but if you are creating json file dynamically then create an file in SDCARD – ρяσѕρєя K Dec 11 '12 at 06:04
  • @posper : it is static. Ok where do i place it? assets or raw? And how do i read the file from there? – Aakash Anuj Dec 11 '12 at 06:07
  • 2
    @AakashAnuj : see Faizan answer and put you file in assets folder – ρяσѕρєя K Dec 11 '12 at 06:08
  • 17
    Don't know why this was closed. The question seems perfectly clear and precise to me. 1) Where are json data files stored when developing an Android app. 2) How do I read that data? Sure, this could be broken up into two questions, but the two questions are so very related (you will never use one without the other) one might as well ask both, which AakashAnuj did. Furthermore, @Faizan did a great job answering it, which invalidates the "unanswerable" complaint. Verdict? **Valid Question** – SMBiggs Jun 25 '16 at 03:41

1 Answers1

228

Put that file in assets.

For project created in Android Studio project you need to create assets folder under the main folder.

Read that file as:

public String loadJSONFromAsset(Context context) {
        String json = null;
        try {
            InputStream is = context.getAssets().open("file_name.json");

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

            json = new String(buffer, "UTF-8");


        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }

and then you can simply read this string return by this function as

JSONObject obj = new JSONObject(json_return_by_the_function);

For further details regarding JSON see http://www.vogella.com/articles/AndroidJSON/article.html

Hope you will get what you want.

Nigam Patro
  • 2,760
  • 1
  • 18
  • 33
Faizan
  • 3,512
  • 2
  • 18
  • 15
  • 3
    what if file size is almost 6 to 7 MB? if store the file contain into string it throws an exception(out of memory). Is there any way to retrieve only one jsonObject at a time from JsonArray file? – Hiren Dabhi Oct 05 '13 at 05:02
  • How do i read a JSON file from a web link? – Si8 Nov 08 '13 at 20:02
  • 8
    _For project created in Android Studio project you need to create assets folder under the main folder_ what is `asset folder` and `main folder` ? – Hirdesh Vishwdewa Nov 02 '15 at 11:52
  • 4
    Allocating the buffer based on the result returned by available() is incorrect as per the Java docs: http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available() "Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream." – Andrew Nov 04 '15 at 22:53
  • @SiKni8 See here: http://stackoverflow.com/a/9606629/1536976 – NoDataDumpNoContribution Nov 29 '15 at 10:26
  • 'For project created in Android Studio project you need to create assets folder under the main folder.'? How to create it and what is the main folder ? – Moeez Feb 26 '17 at 09:55
  • Hi all, this code take a lot of time to read the json file approximately it takes 5 to 7 secs... Is there any alternate way? Please let me know Thanks in advance. – Thirumal Govindaraj Nov 17 '19 at 06:08
  • 2
    This quick tutorial explains how to create the asset folder in Android Studio: http://code2care.org/2015/create-assets-folder-in-android-studio/ – Aaron Campbell Nov 25 '19 at 18:27