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!
json file is generally placed in the app/ directory (at the root of the Android Studio app module).
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.
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")
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With