I will be using a local JSON file with some data. My question is where is the best/proper way to store the file and how should I access it and it's contents?
I have worked with json through http before but not a local one.
Use the require() Function to Load JSON Files in JavaScript In JavaScript, we can use the require() method to load files and modules. This takes the path of the local file where it has been saved. With the help of the console. log() function, it loads the data in the server and displays it.
Where to put assets folder and JSON file. You will need to create the assets folder inside src/main, together with java and res folder. Then put JSON file inside assets folder.
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.
There are so many ways,
You can store your JSON file in assets folder and read them like this - https://stackoverflow.com/a/19945484/713778
You can store it in res/raw folder and read the same as show here - https://stackoverflow.com/a/6349913/713778
For basic JSON parsing, Android's in-built JSONObject should work - https://developer.android.com/reference/org/json/JSONObject.html
For more advanced JSON parsing (json-java mapping), you can look at GSON library - https://code.google.com/p/google-gson/
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