使用Json-lib生成JSON文本

Json-lib测试类,如下

package com.jadyer.json; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import com.jadyer.model.Student; import com.jadyer.model.Teacher; /** * Java对JSON的支持不止体现在Json-lib上面,更多信息请访问http://www.json.org * @see 另外还有一款更为强悍的工具包Jackson,其官方网站为http://jackson.codehaus.org * @see 网上曾有人做过测试,得到结论Jackson>Gson>Json-lib,详情参见http://wangym.javaeye.com/blog/738933 * @see Json-lib可以将Java中的entity、map、collection、数组和XML转换成JSON数据格式 * @see 注意json-lib-2.4-jdk15.jar还需要依赖其它的Jar包 * @see 依赖ezmorph-1.0.6、commons-lang-2.5、commons-beanutils-1.8.0、commons-collections3.2.1、commons-logging-1.1.1 * @see Json-lib的官方网站为http://json-lib.sourceforge.net * @see ezmorph的官方网址为http://ezmorph.sourceforge.net */ public class JsonLibDemo { public static void main(String[] args) { testJavaBean(); testMap(); testArray(); } public static void testJavaBean(){ Student stu = new Student("沈浪", 22); JSONObject jsonObject = JSONObject.fromObject(stu); System.out.println(jsonObject.toString()); Teacher teacher = new Teacher(); teacher.setName("张起灵"); teacher.getStudents().add(new Student("吴邪", 25)); teacher.getStudents().add(new Student("黑眼镜", 26)); teacher.getStudents().add(new Student("陈文锦", 27)); JSONObject jsonObject22 = JSONObject.fromObject(teacher); System.out.println(jsonObject22.toString()); } public static void testMap(){ Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "闷油瓶"); map.put("favorate", new String[]{"睡觉", "发呆", "耍酷"}); map.put("getFavorate", "function(i){return this.favorate[i]}"); JSONObject jsonObject = JSONObject.fromObject(map); System.out.println(jsonObject); } public static void testArray(){ String[] users = {"赵红兵", "小北京", "东霸天"}; JSONArray jsonArray = JSONArray.fromObject(users); System.out.println(jsonArray.toString()); Integer[] numbers = {1, 2, 3}; jsonArray = JSONArray.fromObject(numbers); System.out.println(jsonArray.toString()); Boolean[] booleanArray = {true, false, false, true}; jsonArray = JSONArray.fromObject(booleanArray); System.out.println(jsonArray.toString()); List<String> list = new ArrayList<String>(); list.add("张小凡"); list.add("林惊羽"); list.add("万人往"); jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray.toString()); Set<Student> students = new HashSet<Student>(); students.add(new Student("卢易之", 25)); students.add(new Student("玉姣龙", 26)); students.add(new Student("金羽凤", 27)); jsonArray = JSONArray.fromObject(students); System.out.println(jsonArray.toString()); } }

 

用到的实体类

package com.jadyer.model; public class Student { private String name; private Integer age; /* 两个属性的getter和setter方法略 */ public Student(){} public Student(String name, Integer age){ this.name = name; this.age = age; } } package com.jadyer.model; import java.util.HashSet; import java.util.Set; public class Teacher { private String name; private Set<Student> students = new HashSet<Student>(); /* 两个属性的getter和setter方法略 */ }

你可能感兴趣的:(json,String,Integer,getter,setter,Numbers)