net.sf.json将string转为map

背景

改一个以前的项目,项目里只有这个包,虽然我想用gson或者fastjson,然而并不想引入新的jar。于是使用这个,特此记录,感觉贼不好用。

实现代码

entity.getData()的值:{aaa:bbb}

JSONObject jsonObject = JSONObject.fromObject(entity.getData());
Map<String, String> result = new HashMap<String, String>();
Iterator<String> iterator = jsonObject.keys();
String key = null;
String value = null;
while (iterator.hasNext()) {
    key = iterator.next();
    value = jsonObject.getString(key);
    result.put(key, value);
}

猜你喜欢

转载自www.cnblogs.com/jichi/p/11929617.html