What the best way to convert from String to HashMap?

Posted by eugenn on Stack Overflow See other posts from Stack Overflow or by eugenn
Published on 2010-06-16T11:46:32Z Indexed on 2010/06/16 11:52 UTC
Read the original article Hit count: 172

Filed under:
|
|
|

I would like to serialize a Java HashMap to string representation. The HashMap will contains only primitive values like string and integer. After that this string will be stored to db. How to restore back the HashMap? Is it make sense to use BeanUtils and interface Converter or use JSON?

For example:

    List list = new ArrayList();
    list.add(new Long(1));
    list.add(new Long(2));
    list.add(new Long(4)); 

    Map map = new HashMap();
    map.put("cityId", new Integer(1));
    map.put("name", "test");
    map.put("float", new Float(-3.2));
    map.put("ids", list);

    map.toString() -> {float=-3.2,ids=[1, 2, 4],name=test,cityId=1}
    map.toJSON ->     {"float":-3.2,"ids":[1,2,4],"name":"test","cityId":1}

© Stack Overflow or respective owner

Related posts about java

Related posts about string