3

I have the following data structure:

"properties": {
    "P6": "head of government",
    "P7": "brother",
    "P9": "sister",
    "P10": "video",
    "P14": "highway marker",
    "P15": "road map",
    "P16": "highway system",
    "P17": "country",
    "P18": "image",
    "P19": "place of birth",
    "P20": "place of death",
    "P21": "sex or gender",
    ...

I'd like to read this in from a file and use it to populate a hashmap of type Map<String,String>.

I tried to do this using gson but was unsuccessful, I feel there must be a simpler way.

Maybe I should read it in and split it up using the pattern matcher or regex?

This was the code I had been using:

        /*
         * P values file
         */
        String jsonTxt_P = null;

        File P_Value_file = new File("properties-es.json");
        //raed in the P values
        if (P_Value_file.exists())
        {
            InputStream is = new FileInputStream("properties-es.json");
            jsonTxt_P = IOUtils.toString(is);
        }
        //
        Gson json_P = new Gson();
        Map<String,String> massive_P_storage_map = new HashMap<String,String>();
        massive_P_storage_map = (Map<String,String>) json_P.fromJson(jsonTxt_P, massive_Q_storage_map.getClass());
        System.out.println(massive_P_storage_map);
  • this may be useful (possible duplicate question): http://stackoverflow.com/questions/21720759/convert-a-json-string-to-a-hashmap – germanio Apr 28 '15 at 16:36
  • Look at the API [here](https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html), `fromJson` first argument is the json string source to convert in to java object, 2nd argument is the actual type of java object you want the final object to be. But you're passing null, so it won't work :) – Arkantos Apr 28 '15 at 16:37
  • it's not null it's getting populated by lines from the file –  Apr 28 '15 at 16:39
  • Ok my bad :) but the 2nd argument you're passing is a map, but the sample response you showed is something else. So either you can have a class with an instance variable `properties` of type `Map` or change you json to just have key/value pairs.. that should work – Arkantos Apr 28 '15 at 16:41
  • I'm kind of confused. I do want key/value pairs. but how to get it? –  Apr 28 '15 at 16:49

1 Answers1

1

Do it this way

BufferedReader reader = new BufferedReader(new FileReader(new File("properties-es.json")));
        Map<String, HashMap<String, Object>> map = 
        new Gson().fromJson(reader, new TypeToken<HashMap<String, HashMap<String, Object>>>() {}.getType());

And this is how to get the value depending on key name

String value = (String) map.get("properties").get("P6");
System.out.println(value);
MCHAppy
  • 1,012
  • 9
  • 15
  • That kind of worked. but I think it read it in in kind of a strange way, like this `{properties={P6=alcalde, P7=hermano, P9=hermana, P10=` but I was thinking, it would be better if it was like a map, with those `p values` as keys, and the words associated with them as values, know what I mean? –  Apr 28 '15 at 16:48
  • @Yamada_Tarō Try using the second option in my answer. – MCHAppy Apr 28 '15 at 16:51
  • it worked, somewhat. it turned out a bit strange, like this: `P466=ocupante, P467=legislado por, P468=rango dan/kyu, P469=lagos y embalses`, but they're sort of like. jumbled together –  Apr 28 '15 at 16:56
  • @Yamada_Tarō I got what you mean. Actually, it is not a strange way because you are trying to print all the map together like so `system.out.println(map);` so you got output like so `{properties={P6=alcalde, P7=hermano, P9=hermana, P10=` .I am pretty sure your map is well organised as keys and values according to json file. – MCHAppy Apr 28 '15 at 16:57
  • how could I search a value, a word, given one of those P values? –  Apr 28 '15 at 17:00
  • @Yamada_Tarō To get string value from HashMap depending on key name, you could do this `String value = (String) map.get("P466");` – MCHAppy Apr 28 '15 at 17:02