Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Convert JSON to HashMap

Help
Mathieu
2008-06-05
2013-04-29
  • Mathieu
    Mathieu
    2008-06-05

        public String serializeMessage(final HashMap<String, String> message)
        {
            JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(message);
            return jsonObject.toString();
        }

        public HashMap<String, String> unserializeMessage(final String infosJsonFromTable)
        {
            if (JSONUtils.mayBeJSON(infosJsonFromTable))
            {
                JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(infosJsonFromTable);
                JsonConfig config = new JsonConfig();
                config.setRootClass(java.util.HashMap.class);
                return (HashMap<String, String>) JSONSerializer.toJava(jsonObject, config);
            }

            //Si le contenu n'est pas une JSONString valide on s'assure de renvoyer qd meme une HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("info", infosJsonFromTable);
            return map;
        }