Skip to content

Instantly share code, notes, and snippets.

@andreyserdjuk
Last active August 29, 2015 14:20
Show Gist options
  • Save andreyserdjuk/e9b31072c2a6685fd8a3 to your computer and use it in GitHub Desktop.
Save andreyserdjuk/e9b31072c2a6685fd8a3 to your computer and use it in GitHub Desktop.
json to hashmap
/*
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