今天看easyui,要返回json格式的数据到页面上。
1.json相关的6个包,要全部导入。
2.测试
package testPackage; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; public class TestJson { public static void main(String[] args) { // TODO Auto-generated method stub List<Student> sList = new ArrayList<Student>(); sList.add(new Student("张三",23)); sList.add(new Student("李四",15)); sList.add(new Student("王五",45)); sList.add(new Student("赵六",34)); System.out.println(JSONArray.fromObject(sList).toString()); } }
package testPackage; public class Student{ private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
3.结果
4.注意
1. 测试代码中的student类必须为public,否则会报错Caused by: java.lang.NoSuchMethodException: Property ‘age‘ has no getter method
2. 6个包要全部导入。
原文:http://www.cnblogs.com/yeming/p/5281946.html