json字符串相关转换方法
2015-10-26 17:52
627 查看
/** json转换为Map * @param jsonStr json * @return map集合 */ public static HashMap<String, String> json2HashMap(String jsonStr) { HashMap<String, String> data = new HashMap<String, String>(); // 将json字符串转换成jsonObject JSONObject jsonObject = JSONObject.fromObject(jsonStr); Iterator<Object> it = jsonObject.keys(); // 遍历jsonObject数据,添加到Map对象 while (it.hasNext()) { String key = String.valueOf(it.next()); Object value = jsonObject.get(key); data.put(key, value.toString()); } return data; } public static String toJson (Object object, DateFormat dateFormat) throws IOException { ObjectMapper mapper = JacksonMapper.getInstance(); // 解决hibernate延迟加载 mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); if (dateFormat != null) { mapper.setDateFormat(dateFormat); } else { mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); } String json = getJsonStr(mapper, object); return json; }
相关文章推荐
- JSP开发c标签易错总结
- JavaScript TypeArray
- JS - RegExp 笔记
- 常用的API接口,返回JSON格式的服务API接口
- js弹出框、对话框、提示框、弹窗总结
- 实现jsp页面显示用户登录信息,利用session保存。
- 【bzoj1823】【jsoi2010】【满汉全席】【2-sat】
- JSON串常用函数
- JSON解析详解(JSONKit、苹果自带解析)和PList文件的反序列化
- 用javascript如何实现全选和全不选
- js中substr和substring方法的区别
- JavaScript编程中window的location与history对象详解
- javascript推荐书籍
- 详解JavaScript编程中的window与window.screen对象
- javascript中的关键字和保留字
- 【Cell报表】与JS的千丝万缕
- JS常用方法
- 当在浏览器地址栏输入一个网址的时候,究竟发生了什么?
- json validator,流事处理,字符事件驱动,状态维护
- html5检测浏览器是否支持视频播放js代码