java - populate a hashmap with json data from file -
i want represent this file in java program.
what want search through "key" value, instance, given value p26
i'd want return spouse
.
i thought in 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 value depending on key name
string value = (string) map.get("properties").get("p6"); system.out.println(value);
but when run first bit, error:
expected begin_array string @ line 1347 column 10
that part of a file modified simplify data structure looks this: (^was bad idea? original file suitable represented in hashmap i'm describing?)
"p1655": "station number", "p1656": "unveiled by", "p1657": "mpaa film rating", "p1658": "number of faces" }
i did tha because original file seems more complex data structure. said before, want enable ability query "p values"
it's idea hashmap p values keys , words values best way this, how can make happen?
heretofore i've been using gson.
your typetoken doesn't match structure of file contents, array ("missing").
you create data class same fields file, or keep things simple , do:
bufferedreader reader = new bufferedreader(new filereader(new file("properties-es.json"))); map<string, map<string, object>> map = new gson().fromjson(reader, hashmap.class);
you'll compiler warning, can suppressed, should work fine.
with data class:
// if inner class add "static" public class data { public map<string,string> properties; public list<string> missing; } //... data data = new gson().fromjson(reader, data.class);
Comments
Post a Comment