Search code examples
javajsonconfigurationjson-simple

Path with dots for Maps in JSON (Java)


I want to make it so I can use "." to obtain values within a Map within the main Map in a JSON file.

E.g. My file looks like this: http://pastie.org/private/wl6jhgnylevz2fdcncyjrg

And in Java, I load the configuration to a Map (but I can always use JSONObject if necessary to do the following). But I want to get the value of "One" in the "Test" map using a get method that allows me to type: config.get("Test.One");

Here's what I currently have: http://pastie.org/private/zkxbxgs2nsteb1g0zcp6tq

It just doesn't make sense (what I wrote), doesn't work and I pretty much just wrote a load of crap. I'm really confused as to how to do this, any help would be appreciated. I'm using JSON-Simple.


Solution

  • Have you considered using a 3rd party libraries like Json.org or Gson to serialize/deserialize your object into/from json?

    That way you forget about the hassle of managinf strings and JSons and just use objects straight up. I dont know if you'll get the path you are looking for you out of the box should easily achieve something like config.test.one.