申明:没工作之前都没听过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包:

  1. commons-lang.jar
  2. commons-beanutils.jar
  3. commons-collections.jar
  4. commons-logging.jar
  5. ezmorph.jar
  6. 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对象的使用的更多相关文章

  1. 在java中构建json对象,返回给前端页面

    // 给客户端返回一个json对象 StringBuilder sb = new StringBuilder("{"); sb.append("\"name\& ...

  2. JAVA中使用JSON进行数据传递

    最近在做一个基于JAVA Servlet的WEB应用以及对应的Anroid应用客户端的开发工作. 其中,在接口的访问和数据的传输方面使用的比较多的是使用JSON对象来操作格式化数据:在服务器端采用JS ...

  3. 转载:JAVA中使用JSON进行数据传递

    转载网址:http://www.cnblogs.com/undead/archive/2012/07/18/2594900.html 最近在做一个基于JAVA Servlet的WEB应用以及对应的An ...

  4. JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson

    java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...

  5. Java中哪个JSON库的解析速度是最快的?

    JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考 了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上 ...

  6. 3、示例(在java中使用JSON)

    教程链接(json-smple1.1.1.jar文件) 链接:http://pan.baidu.com/s/1qXPbYHm 密码:v0f0 如何使用java编程语言编码和解码JSON 首先准备环境以 ...

  7. MVC中处理Json和JS中处理Json对象

    MVC中处理Json和JS中处理Json对象 ASP.NET MVC 很好的封装了Json,本文介绍MVC中处理Json和JS中处理Json对象,并提供详细的示例代码供参考. MVC中已经很好的封装了 ...

  8. js中的json对象详细介绍

    JSON一种简单的数据格式,比xml更轻巧,在JavaScript中处理JSON数据不需要任何特殊的API或工具包,下面为大家详细介绍下js中的json对象, 1.JSON(JavaScript Ob ...

  9. java中对集合对象list的几种循环访问

    java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static void main(String[] args) { List<String> li ...

随机推荐

  1. CodeForces Round #179 (295A) - Greg and Array

    题目链接:http://codeforces.com/problemset/problem/295/A 我的做法,两次线段树 #include <cstdio> #include < ...

  2. hdoj 1406 完数

    完数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  3. hdoj 3572 Task Schedule【建立超级源点超级汇点】

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. 使用ajax代替iframe

    相信大多数程序员都跟iframe打过交道,iframe简单,好用.在我用的过程中比较苦逼的是关于iframe高度的设置. 由于子页面内容不确定,页面高度也不确定.于是开始网上的各种搜索,一般有两种:一 ...

  5. C#.bat文件清理工程目录

    另外一种方法是自己写一个bat文件来清理,非常方便,下面是自己写的验证过比较好用的方法. bat文件内容如下: echo 正在清理VS2010工程中不需要的文件 echo 请确保本文件放置在工程目录之 ...

  6. [caffe]深度学习之图像分类模型VGG解读

    一.简单介绍 vgg和googlenet是2014年imagenet竞赛的双雄,这两类模型结构有一个共同特点是go deeper.跟googlenet不同的是.vgg继承了lenet以及alexnet ...

  7. ioc容器

    对于容器而言需要满足两个方面: 1.全局唯一 2.无论何地都可以进行对容器的访问 对于Spring而言,BeanFactory则就是这样的容器,只不过它过于底层.在我们的日常开发中还是使用Applic ...

  8. 通过blktrace, debugfs分析磁盘IO

    前几天微博上有同学问我磁盘util达到了100%时程序性能下降的问题,由于信息实在有限,我也没有办法帮太大的忙,这篇blog只是想给他列一下在磁盘util很高的时候如何通过blktrace+debug ...

  9. bind的例子

    10.24 给定一个string,使用bind和check_size在一个int的vector中查找第一个大于string长度的值. #include<iostream> #include ...

  10. 适用于cocos2dx的编辑器:Texture,Tilemap,Particle,Action,Level etc

    原文:http://www.cocos2d-x.org/wiki/Editors_for_cocos2d-x_TextureTilemapParticleActionLevel_etc Action ...