申明:没工作之前都没听过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. Redis+Spring缓存实例(windows环境,附实例源码及详解)

    原文出处: 小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). ...

  2. Scala IDE for Eclipse的下载、安装和WordCount的初步使用(本地模式和集群模式)

    包括: Scala IDE for Eclipse的下载  Scala IDE for Eclipse的安装 本地模式或集群模式 我们知道,对于开发而言,IDE是有很多个选择的版本.如我们大部分人经常 ...

  3. XXX 用户 is not in the sudoers file. This incident will be reported 的问题解决方案

    说的是,这种问题,是出现在ubuntu系统里. root@SparkSingleNode:/usr/local/jdk# pwd /usr/local/jdk root@SparkSingleNode ...

  4. js基础一

    1.声明提升:变量的声明提升,函数的声明提升,但函数赋值表达式不会提升: foo(); // 正常运行,因为foo在代码运行前已经被创建 function foo() {} foo(); // 出错: ...

  5. BZOJ 3280: 小R的烦恼 & BZOJ 1221: [HNOI2001] 软件开发

    3280: 小R的烦恼 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 399  Solved: 200[Submit][Status][Discuss ...

  6. JS正则表达式验证表单

    一.解释一些符号相关的意义     1.  /^$/ 这个是个通用的格式.         ^ 匹配输入字符串的开始位置:$匹配输入字符串的结束位置     2. 里面输入需要实现的功能.       ...

  7. Integer Inquiry_hdu_1047(大数).java

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 【C语言】编写一个函数实现n^k,使用递归实现

    #include <stdio.h> int fuc(int x,int n) { if(n!=1) return x*fuc(x,n-1); return 1; } int main() ...

  9. android89 服务service

    #服务 服务不能new,new出来的只是一个普通java对象不是服务,只能够通过Intent和startService(intent)创建服务. ###开启方式 * startService,onCr ...

  10. Java编程最差代码

    字符串连接误用 错误的写法:  String s = ""; for (Person p : persons) { s += ", " + p.getName( ...