Java中的JSON对象的使用
作者:网络转载 发布时间:[ 2014/9/15 11:29:39 ] 推荐标签:软件开发 Java 编程
申明:没工作之前都没听过JSON,可能是自己太菜了。可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式。
网上有两种解析JSON对象的jar包:JSON-lib.jar和json.jar,这里主要介绍JSON-lib.jar。
jar包地址如下:
json-lib-2.4-jdk15.jar所需全部JAR包.rar
一、JSON-lib.jar还依赖以下jar包:
commons-lang.jar
commons-beanutils.jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
json-lib-2.2.2-jdk15.jar
二、应用
JSON也是以key-value形式存在的。key是字符串,value可以是基本类型、JSONArray、JSONObject.
JSONArray:[],望文生义也知道,他是数组形式,又可要放多个JSON
JSONObject:{}放一个JSON。
由于他们的他们可以嵌套形式比较多。
三、输出JSON实例考虑到对[]、{}进行对比,区别重复的变量,对变量名进行了首字母大写,显得不规范了。
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONTest {
public static void main(String[] args) {
JSONObject container1 = new JSONObject();
container1.put("ClassName", "高三一班");
System.out.println(container1.toString());
JSONArray className = new JSONArray();
className.add("高三一班");
container1.put("className", className);
System.out.println(container1.toString());
JSONObject classInfo = new JSONObject();
classInfo.put("stuCount", 50);
classInfo.put("leader", "rah");
container1.put("classInfo", classInfo);
System.out.println(container1);
JSONObject ClassInfo = new JSONObject();
JSONArray stuCount = new JSONArray();
stuCount.add(50);
JSONArray leader = new JSONArray();
leader.add("rah");
ClassInfo.put("stuCount", stuCount);
ClassInfo.put("leader", leader);
container1.put("ClassInfo", ClassInfo);
System.out.println(container1);
JSONArray students = new JSONArray();
JSONObject studentOne = new JSONObject();
studentOne.put("name", "张麻子");
studentOne.put("sex", "男");
studentOne.put("age", 12);
studentOne.put("hobby", "java develop");
JSONObject studentTwo = new JSONObject();
studentTwo.put("name", "王瘸子");
studentTwo.put("sex", "男");
studentTwo.put("age", 13);
studentTwo.put("hobby", "C/C++ develop");
students.add(studentOne);
students.add(studentTwo);
container1.put("students", students);
System.out.println(container1);
JSONArray Students = new JSONArray();
JSONObject StudnetOne = new JSONObject();
JSONArray name1 = new JSONArray();
name1.add("张麻子");
JSONArray sex1 = new JSONArray();
sex1.add("男");
JSONArray age1= new JSONArray();
age1.add("12");
JSONArray hobby1 = new JSONArray();
hobby1.add("java develop");
StudnetOne.put("name", name1);
StudnetOne.put("sex", sex1);
StudnetOne.put("age", age1);
StudnetOne.put("hobby", hobby1);
JSONObject StudnetTwo = new JSONObject();
JSONArray name2 = new JSONArray();
name2.add("王瘸子");
JSONArray sex2 = new JSONArray();
sex2.add("男");
JSONArray age2= new JSONArray();
age2.add("13");
JSONArray hobby2 = new JSONArray();
hobby2.add("C/C++ develop");
StudnetTwo.put("name", name2);
StudnetTwo.put("sex", sex2);
StudnetTwo.put("age", age2);
StudnetTwo.put("hobby", hobby2);
Students.add(StudnetOne);
Students.add(StudnetTwo);
container1.put("Students", Students);
System.out.println(container1);
JSONArray teachers = new JSONArray();
teachers.add(0,"王老师");
teachers.add(1,"李老师 ");
container1.put("teachers", teachers);
System.out.println(container1);
JSONArray Teachers = new JSONArray();
JSONObject teacher1 = new JSONObject();
teacher1.put("name", "小梅");
teacher1.put("introduce","他是一个好老师");
JSONObject teacher2 = new JSONObject();
teacher2.put("name", "小李");
teacher2.put("introduce","他是一个合格的老师");
Teachers.add(0,teacher1);
Teachers.add(1,teacher2);
container1.put("Teachers", Teachers);
System.out.println(container1);
}
}

sales@spasvo.com