0

Is it possible to load JSON file once, and in further call will use loaded JSON (for ex. from cache)?

muzafako
  • 188
  • 2
  • 11

2 Answers2

0

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');
0

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/