Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add json file to android project

Tags:

I am simply trying to add a .json file into my android studio project. Most of what I have run into has you create a folder and add it there but I want to simply move it over under app. Here Is my project image

How to add JSON files to my project

Add JSON file to app

like image 712
Jeff Louzada Avatar asked Nov 12 '16 16:11

Jeff Louzada


People also ask

Where is the JSON file stored in Android Studio?

1 Answer. Show activity on this post. You need to store you json file in assets folder.

Where do I put JSON files?

Basically you can put them anywhere and adress them there. Tho perhaps you may want to give them in as a startargument? Your program will look for a file named "test. json" in the current directory.

Can JSON be used in mobile application?

Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and all other forms to parse the JSON data and fetch the required information by the program. JSON's main advantage is that it is a language-independent, and the JSON object will contain data like a key/value pair.


2 Answers

I want to simply move it over under app

If you literally mean that you want to have app/something.json, you are welcome to put the file there, but it will not be packaged with your app, and it will not be available to you at runtime.

If you want to ship the JSON with your app, you have four major options:

  • Put it in assets/ and read it in using AssetManager and its open() method to get an InputStream

  • Put it in res/raw/ and read it in using Resources and its openRawResource() method

  • Hardcode it as a string in Java code

  • Write yourself a code generator that converts JSON into a Java class that you would access like you do the code-generated R and BuildConfig classes

It is possible that such a code generator already exists. I have a rudimentary Gradle plugin that does this, as an example that I'll be including in the next update of my book.

like image 196
CommonsWare Avatar answered Sep 30 '22 21:09

CommonsWare


Please follow the steps below and json file in our project.

step 1 : Change Android to Project

Step 2 : create assets Folder and put json file like login.json

enter image description here

I hope it will help you...!

like image 23
Viral Patel Avatar answered Sep 30 '22 21:09

Viral Patel