0

Im trying to decode json string to Map.

I know there were many questions like this, but I need quite specific format. For example, I have json string:

{
 "map": {
 "a": "b",
 "c": "d",
 },
 "map2": {
 "aa": "bb",
 "cc": "dd",
 },
 "something": "a",
 "something2": "b"
}

And I need to have results like:

"map.a" => "b"
"map.c" => "d"
"map2.aa" => "bb"
"map2.cc" => "dd"
"something" => "a"
"something2" => "b"

Im sure that the keys won't contain any dots. I looked at few JSON libraries, but I don't need so many functions, just to decode and store in Java map. If there is no simple way, I gonna write own algorithm for this, I hope it won't be so hard...

Thanks for any help.

asked Apr 2, 2013 at 14:59
1
  • Even when you use your own algorithm to generate the keys in dot-notation, I would still recommend you to use a decoder library. There are too many edge-cases which could cause nasty bugs, like string escaping rules. Commented Apr 2, 2013 at 15:03

3 Answers 3

1

I used org.codehaus.jackson. Do the following:

 HashMap<String, Object> content = null;
 HashMap<String, String> result = new HashMap<String, String>();
 try {
 JsonFactory factory = new JsonFactory();
 ObjectMapper mapper = new ObjectMapper(factory);
 TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
 };
 content = mapper.readValue(jsonString, typeRef);
 } catch (Exception ex) {
 System.out.println("Exception : " + ex);
 } 
 // now content has everything inside
 for(String s : content.keySet()){
 Object obj = content.get(s);
 if(obj instanceof String){
 result.put(s, (String)obj);
 } else {
 HashMap<String,String> hm = (HashMap<String,String>)obj;
 for(String s2: hm.keySet()){
 result.put(s+"."+s2, hm.get(s2));
 }
 }
 }

Edit: Tested, and working

answered Apr 2, 2013 at 15:36
Sign up to request clarification or add additional context in comments.

Comments

1

To accurately parse JSON you'll want to define a proper parser for it, they're not terribly difficult to write just time consuming. My recommendation would be to find a fairly lightweight JSON parser and then write a decorator for it to get the map format you're looking for. Although, if you're processing large amounts of JSON you'll, unfortunately, be adding overhead.

answered Apr 2, 2013 at 15:04

Comments

1

I don't think you'll find an out-of-the-box solution to this, since that's a non-standard representation. My suggestion would be to use a JSON library to convert the JSON string to a Java Map, then do a depth-first traversal of that Map to transform it to your desired representation.

answered Apr 2, 2013 at 15:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.