Is it possible to load JSON file once, and in further call will use loaded JSON (for ex. from cache)?
-
Please post some relevant code for better understanding the issue – RahulB Jul 04 '18 at 05:24
-
Its very hard to answer this question since many would find it really hard to understand, i know Im having a hard time. https://stackoverflow.com/help/how-to-ask – Dhanushka Dolapihilla Jul 04 '18 at 05:40
-
I simplified the question – muzafako Jul 04 '18 at 05:48
-
Why not just store the JSON in a variable? – Jacques Marais Jul 04 '18 at 06:02
-
@JacquesMarais the storage is 5MB and not enough for using – muzafako Jul 04 '18 at 06:38
2 Answers
The easiest way to achieve this is to use the localStorage
of JavaScript.
Let's assume you have an object named object
.
You can store it this way:
// parse Object to JSON, then store it.
let jsonObject = JSON.stringify(object);
localStorage.setItem('myObject', jsonObject);
And if you want to use it again, do it this way:
// load it from storage and parse the JSON to Object.
let jsonObject = localStorage.getItem('myObject');
let object = null;
if(jsonObject){
object = JSON.parse(jsonObject);
}
Then change it according to your needs and store it again.
You can delete it by
localStorage.removeItem('myObject');
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/

- 11
- 4