首页 > 代码库 > java中的JSON对象的使用
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的嵌套形式,可能有忽略的也可以参考上面的内容。
java中的JSON对象的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。