Last active
August 29, 2015 14:20
-
-
Save andreyserdjuk/e9b31072c2a6685fd8a3 to your computer and use it in GitHub Desktop.
json to hashmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
jackson-mapper here: | |
http://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl | |
*/ | |
import org.codehaus.jackson.JsonParseException; | |
import org.codehaus.jackson.map.JsonMappingException; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.codehaus.jackson.type.TypeReference; | |
public static HashMap<String, String> jsonToHashMap(String jsonSource) { | |
HashMap<String, String> out = new HashMap<String, String>(); | |
try { | |
// 1 get any, even non-valid json data | |
// 2 -> to hashmap | |
JSONObject o = new JSONObject(jsonSource); | |
out = new ObjectMapper().readValue(o.toString(), new TypeReference<HashMap<String,String>>(){}); | |
} catch (JSONException e1) { | |
e1.printStackTrace(); | |
} catch (JsonMappingException e1) { | |
e1.printStackTrace(); | |
} catch (JsonParseException e1) { | |
e1.printStackTrace(); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment