qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java中的JSON對象的使用

          申明:沒工作之前都沒聽過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);
          }
          }
          運行結果:
          {"ClassName":"高三一班"}
          {"ClassName":"高三一班","className":["高三一班"]}
          {"ClassName":"高三一班","className":["高三一班"],"classInfo":{"stuCount":50,"leader":"rah"}}
          {"ClassName":"高三一班","className":["高三一班"],"classInfo":{"stuCount":50,"leader":"rah"},"ClassInfo":{"stuCount":[50],"leader":["rah"]}}
          {"ClassName":"高三一班","className":["高三一班"],"classInfo":{"stuCount":50,"leader":"rah"},"ClassInfo":{"stuCount":[50],"leader":["rah"]},"students":[{"name":"張麻子","sex":"男","age":12,"hobby":"java develop"},{"name":"王瘸子","sex":"男","age":13,"hobby":"C/C++ develop"}]}
          {"ClassName":"高三一班","className":["高三一班"],"classInfo":{"stuCount":50,"leader":"rah"},"ClassInfo":{"stuCount":[50],"leader":["rah"]},"students":[{"name":"張麻子","sex":"男","age":12,"hobby":"java develop"},{"name":"王瘸子","sex":"男","age":13,"hobby":"C/C++ develop"}],"Students":[{"name":["張麻子"],"sex":["男"],"age":["12"],"hobby":["java develop"]},{"name":["王瘸子"],"sex":["男"],"age":["13"],"hobby":["C/C++ develop"]}]}
          {"ClassName":"高三一班","className":["高三一班"],"classInfo":{"stuCount":50,"leader":"rah"},"ClassInfo":{"stuCount":[50],"leader":["rah"]},"students":[{"name":"張麻子","sex":"男","age":12,"hobby":"java develop"},{"name":"王瘸子","sex":"男","age":13,"hobby":"C/C++ develop"}],"Students":[{"name":["張麻子"],"sex":["男"],"age":["12"],"hobby":["java develop"]},{"name":["王瘸子"],"sex":["男"],"age":["13"],"hobby":["C/C++ develop"]}],"teachers":["王老師","李老師 "]}
          {"ClassName":"高三一班","className":["高三一班"],"classInfo":{"stuCount":50,"leader":"rah"},"ClassInfo":{"stuCount":[50],"leader":["rah"]},"students":[{"name":"張麻子","sex":"男","age":12,"hobby":"java develop"},{"name":"王瘸子","sex":"男","age":13,"hobby":"C/C++ develop"}],"Students":[{"name":["張麻子"],"sex":["男"],"age":["12"],"hobby":["java develop"]},{"name":["王瘸子"],"sex":["男"],"age":["13"],"hobby":["C/C++ develop"]}],"teachers":["王老師","李老師 "],"Teachers":[{"name":"小梅","introduce":"他是一個好老師"},{"name":"小李","introduce":"他是一個合格的老師"}]}
            四、遍歷JSON實例
            以上面的輸出的JSON字符串進行按順序給它遍歷
            String ClassName1 = (String) container1.get("ClassName");
            System.out.println("ClassName data is: " + ClassName1);
            JSONArray className1 = container1.getJSONArray("className");
            System.out.println("className data is: " + className1);
            JSONObject classInfo1 = container1.getJSONObject("classInfo");
            System.out.println("classInfo data is: " + classInfo1);
            JSONObject ClassInfo1 = container1.getJSONObject("ClassInfo");
            System.out.println("ClassInfo data is: " + ClassInfo1);
            JSONArray students1 = container1.getJSONArray("students");
            System.out.println("students data is: " + students1);
            JSONArray Students1 = container1.getJSONArray("Students");
            System.out.println("Students data is: " + Students1);
            JSONArray teachers1 = container1.getJSONArray("teachers");
            for(int i=0; i < teachers1.size(); i++){
            System.out.println("teahcer " + i + " is: "+ teachers1.get(i));
            }
            JSONArray Teachers1 = container1.getJSONArray("Teachers");
            for(int i=0; i < Teachers1.size(); i++){
            System.out.println("Teachers " + i + " is: "+ Teachers1.get(i));
            }
            遍歷結果:
            ClassName data is: 高三一班
            className data is: ["高三一班"]
            classInfo data is: {"stuCount":50,"leader":"rah"}
            ClassInfo data is: {"stuCount":[50],"leader":["rah"]}
            students data is: [{"name":"張麻子","sex":"男","age":12,"hobby":"java develop"},{"name":"王瘸子","sex":"男","age":13,"hobby":"C/C++ develop"}]
            Students data is: [{"name":["張麻子"],"sex":["男"],"age":["12"],"hobby":["java develop"]},{"name":["王瘸子"],"sex":["男"],"age":["13"],"hobby":["C/C++ develop"]}]
            teahcer 0 is: 王老師
            teahcer 1 is: 李老師
            Teachers 0 is: {"name":"小梅","introduce":"他是一個好老師"}
            Teachers 1 is: {"name":"小李","introduce":"他是一個合格的老師"}
            上面包括了大部份的JSON的嵌套形式,可能有忽略的也可以參考上面的內容。

          posted on 2014-09-16 09:52 順其自然EVO 閱讀(260) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 漳浦县| 上虞市| 绥化市| 邓州市| 兴化市| 吴堡县| 寻乌县| 曲周县| 庆云县| 丹棱县| 伽师县| 巴林右旗| 阿拉善左旗| 南皮县| 凤凰县| 射洪县| 凤山县| 祁东县| 潜江市| 资中县| 广饶县| 邯郸市| 武邑县| 湘潭市| 吉安县| 漠河县| 全南县| 察隅县| 邵东县| 商洛市| 岳西县| 高陵县| 博罗县| 东台市| 九龙坡区| 伽师县| 托里县| 湟源县| 常熟市| 岚皋县| 白银市|