Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using local json file in Android

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.

like image 208
user3658609 Avatar asked May 22 '14 13:05

user3658609


People also ask

How do I run a local JSON file?

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 do I put JSON files on Android?

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.

Does JSON work on Android?

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.


1 Answers

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/

like image 114
ranjk89 Avatar answered Oct 06 '22 13:10

ranjk89