How can I convert json string to Java Map using JSON-lib(http://json-lib.sourceforge.net/) ? I can convert to DynaBean:
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str );  
DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject );  
but I have not found a method to convert directly to Java Map
Edit:
I have found the solution:
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str );
Map<String, Object> myMap = (Map<String, Object>) JSONObject.toBean(jsonObject, Map.class)
                Another approach is to take advantage of the fact that a JSONObject implements java.util.Map.  So, it is a map.
import java.util.Map;
import net.sf.json.JSONObject;
public class Foo
{
  static String json = "{\"one\":\"won\",\"two\":2,\"three\":false}";
  public static void main(String[] args)
  {
    JSONObject jsonObject = JSONObject.fromObject(json);
    Map map = jsonObject;
    System.out.println(map);
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With