Convert HashMap.toString() back to HashMap in Java

后端 未结 12 1737
执念已碎
执念已碎 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 01:16

    Are you restricted to use only HashMap ??

    Why can't it be so much flexible JSONObject you can do a lot with it.

    You can convert String jsonString to JSONObject jsonObj

    JSONObject jsonObj = new JSONObject(jsonString);
    Iterator it = jsonObj.keys();
    
    while(it.hasNext())
    {
        String key = it.next().toString();
        String value = jsonObj.get(key).toString();
    }
    

提交回复
热议问题