0

In my application I've got this big json file (7947 rows) with a very simple structure, which pairs each Italian City with its Codice Catastale.

CodiciCatastali.json

...      
"Villamar": "L966",
"Villamassargia": "L968",
"Villanova Tulo": "L992",
"Villanovaforru": "L986",
"Villanovafranca": "L987",
"Villaperuccio": "M278",
"Villaputzu": "L998",
...

I've already used it in JS and it was easy to load it.

Now I'd like to use it in my android application but I can't figure out a couple of things.

First of all, where should I save the file? I red about Raw folder or Assets folder but which solution is the best?

And then, how do I load it inside an object like HashMap? I need to look for the codes once given the City name. ("key"-->"Value")

Thanks for the help.

EDIT

Thanks to Hamid Fadishei I updated the code:

HashMap map = new HashMap<String, String>();
    try {
        InputStream is = getResources().openRawResource(R.raw.CodiciCatastali);
        JsonReader reader = new JsonReader(new InputStreamReader(is));
        map = new Gson().fromJson(reader, HashMap.class);
        } catch (Exception e) {
        e.printStackTrace();
    }

But now I can't call getResources() becouse it ask me for a context. Anyway I thought to pass a context from the main app to the module but this will give me the context of the application and not the one of the library, in which my file.json is stored.

SECOND EDIT

I tried storing the file in raw folder of library, and then access it using Context from main app, and unexpectedly It worked! So now I can say problem Solved, thanks.

L. Gangemi
  • 3,110
  • 1
  • 22
  • 47

1 Answers1

2

Regarding the difference between assets and raw resources, please refer to The reason for Assets and Raw Resources in Android.

For loading JSON files, you may use a third-party library like Gson. Just add the dependency to build.gradle:

compile 'com.google.code.gson:gson:2.8.5'

Then you can go like this:

InputStream is = getAssets().open("CodiciCatastali.json");
JsonReader reader = new JsonReader(new InputStreamReader(is));
HashMap<String, Integer> map = gson.fromJson(reader, HashMap.class);

Since you have mentioned the large size of the file, it is advised to perform the above operation outside the UI thread.

Hamid Fadishei
  • 830
  • 2
  • 10
  • 16
  • Thanks for the answer, but I can't figure out why "gson.fromJson(reader, HashMap.class);" gives me error "Cannot resolve method 'fromJson(android.util.JsonReader, java.lang.Class)" – L. Gangemi Aug 06 '18 at 23:04
  • Ok, looks like I solved it by directly pass InputStream to "fromJson" instead of the JsonReader. By the way I'm working in a module and I need to access the Module assets folder, do you know how to do it? Thanks – L. Gangemi Aug 06 '18 at 23:21
  • 1
    Asset files are not supported in library projects. Either put your asset in application project or go with the res/raw alternative – Hamid Fadishei Aug 07 '18 at 04:55
  • 1
    Regarding the error on fromJson(), the reason is that you have imported android.util.JsonReader while com.google.gson.stream.JsonReader is required. – Hamid Fadishei Aug 07 '18 at 05:00
  • Ok, I'm trying with res/raw folder, but in the module which is a Library I can't call getResources() to retrive files from raw. Do you know any way? I'll update my question here so that you can see my updated code – L. Gangemi Aug 07 '18 at 09:57