zl程序教程

您现在的位置是:首页 >  后端

当前栏目

net.sf.json将string转为map

NetMapJSONJSON string 转为 SF
2023-09-27 14:21:58 时间

背景

改一个以前的项目,项目里只有这个包,虽然我想用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);
}