1、首先要有 jackson-all-1.8.1.jar包 的支持
2、接下来就是一个简单的demo
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
public class SqlToJson {
public static void main(String[] args) {
List<Person> arrList = new ArrayList<Person>();
Person person = new Person("kang", 24);
arrList.add(person);
String str = null;
try {
str = gnerateJsonFromObject(arrList);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(str);
}
public static String gnerateJsonFromObject(Object object) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(object);
}
}
3、输出结果:
[{"age":24,"name":"kang"}]