Java后台处理Json格式数据的方法
程序员文章站
2024-03-12 14:09:56
1.将对象转换为json字符串,返回值为一个json字符串
public static string tojson(object value) {...
1.将对象转换为json字符串,返回值为一个json字符串
public static string tojson(object value) { try { return mapper.writevalueasstring(value); } catch (exception e) { e.printstacktrace(); } return null; }
2. 将json字符串转换为实体对象,返回值为实体对象
public static <t> t toobject(string json, class<t> valuetype) { assert.hastext(json); assert.notnull(valuetype); try { simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); mapper.setdateformat(dateformat); return mapper.readvalue(json, valuetype); } catch (exception e) { e.printstacktrace(); } return null;
上一篇: PHP使用new StdClass()创建空对象的方法分析
下一篇: 深入理解java三种工厂模式