Convert HashMap.toString() back to HashMap in Java

后端 未结 12 1734
执念已碎
执念已碎 2020-12-14 00:35

I put a key-value pair in a Java HashMap and converted it to a String using the toString() method.

Is it possible to convert t

12条回答
  •  执念已碎
    2020-12-14 00:53

    You can make use of Google's "GSON" open-source Java library for this,

    Example input (Map.toString) : {name=Bane, id=20}

    To Insert again in to HashMap you can use below code:

    yourMap = new Gson().fromJson(yourString, HashMap.class);
    

    That's it Enjoy.

    (In Jackson Library mapper It will produce exception "expecting double-quote to start field name")

提交回复
热议问题