0

i would like to ask, how to store json data. I have a JSON file, which i parse using JSON Library. Now i got the data from a file. But i want to store them and show them later again.

The question is, whats the best way to store data? And is it even worth to store them?

I'm thinking about sql database, because its simple and most used. Official android docs have few examples, so far i searched but if u have better guide, let me know.

Thank you! :)

EDIT1:

Ok, i have json file with data, which i can add to my app using RAW resources. Those data wont change, its a list of recipes, i dont have to download it. I can read the data like this:

InputStream is = mContext.getResources().openRawResource(R.raw.package_01);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
    Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    int n;
    while ((n = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, n);
    }
is.close();
//catchblock
.....
}

and then i can parse the data trought JSONLibrary like this:

try {
    //representing []JSON
    JSONArray jsonArray = new JSONArray(writer.toString());

    if(jsonArray != null){...}
...}

Im sending a HashMap to ListView, which includes name and id. And if the user clicks the ListView/GridView item, there is new Activity started, which shows all parsed data. I need to get match those parsed data with the id.

There are about 200 recipes in the file. The data are parsed on start of the Activity, while Splashscreen is displayed. I´m not sure, if its good idea to parse the data everytime when app starts.

So, is it effitient to parse data everytime the app starts? And if yes, how to keep the parsed data "together"? Should i use HashMap?

I hope i clarified my question :) Thanks for help.

EDIT2:

So after i knew what to do, i tried the suggested solution to use HashMap. Problem was there i got Failed Binder Exception. I have images encoded in Base64, that means i have a very long String, example here. That is a lot of data, there is limit:

The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. I´ve tried to save it to

Set<String> titles = new HashSet<String>();

and then to SharedPreferences but the data for each recipe gets mixed. **So, here it comes again, should i save the data to SQLite database or is there another effective option i can use? ** Thank you!

Community
  • 1
  • 1
Vojtěch Pešek
  • 1,070
  • 8
  • 25

1 Answers1

2

It really depends on a number of things like: How much data do you have now, how much will you have later, how complicated is the data. You could use something as simple as an array or hashmap; or something as complex as a database. You need to consider what you are trying to do , and find the simplest solution. If you are trying to persist data, you could use shared preferences, database, and internal/external storage (options outlined here).

Without more information it's hard to say what exactly to do. Keep it simple though. If you are getting JSON from a web service, I'd use an ArrayList or HashMap to store the data, rather than persisting it. It is simpler to implement and does the job.

EDIT:

To answer your question: Yes, using a HashMap and parsing each time is fine. You only have 200 fields, and you don't have images, so the time it will take to parse is minimal. Regardless of how you store the data, there is going to some level of "parsing" done. For example, if you store the data in a database, you are going to have to still pull the data, and put it into a HashMap.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156