Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include local .json file in Eclipse Android project

I have a local .json file. I don't want it to be on a server, I just want it to be included in my app. I tried to paste it directly into Eclipse in my project, but I got a FileNotFoundException, I also tried to paste it in the workspace folder in Windows Explorer/Finder and got the same exception. Where should I put it?

Thanks!

like image 492
Magnus Avatar asked Aug 18 '11 14:08

Magnus


People also ask

Where do I put JSON files in Android project?

json file is generally placed in the app/ directory (at the root of the Android Studio app module).

Where do I put JSON files in eclipse?

Expand the plugin folder node. Expand the appmodel folder node. Double-click the metadata. json file, or right-click the file and select Open with > Json Editor.


2 Answers

You should put the file either in the /assets or /res/raw directory of your Android project. From there, you can retrieve it with either: Context.getResources().openRawResource(R.raw.filename) or Context.getResources().getAssets().open("filename").

like image 157
Erich Douglass Avatar answered Oct 11 '22 07:10

Erich Douglass


Put the json file in assets folder, I have used this method like this

public static String jsonToStringFromAssetFolder(String fileName,Context context) throws IOException {
        AssetManager manager = context.getAssets();
        InputStream file = manager.open(fileName);

        byte[] data = new byte[file.available()];
        file.read(data);
        file.close();
        return new String(data);
    }

While parsing we can use the method like:

String jsondata= jsonToStringFromAssetFolder(your_filename, your_context);
jsonFileToJavaObjectsParsing(jsondata);  // json data to java objects implementation 

More Info: Prativa's Blog

like image 44
LOG_TAG Avatar answered Oct 11 '22 05:10

LOG_TAG