欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java后台处理Json格式数据的方法

程序员文章站 2024-03-11 22:37:31
 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;