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对象的使用的更多相关文章
- 在java中构建json对象,返回给前端页面
// 给客户端返回一个json对象 StringBuilder sb = new StringBuilder("{"); sb.append("\"name\& ...
- JAVA中使用JSON进行数据传递
最近在做一个基于JAVA Servlet的WEB应用以及对应的Anroid应用客户端的开发工作. 其中,在接口的访问和数据的传输方面使用的比较多的是使用JSON对象来操作格式化数据:在服务器端采用JS ...
- 转载:JAVA中使用JSON进行数据传递
转载网址:http://www.cnblogs.com/undead/archive/2012/07/18/2594900.html 最近在做一个基于JAVA Servlet的WEB应用以及对应的An ...
- JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson
java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...
- Java中哪个JSON库的解析速度是最快的?
JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考 了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上 ...
- 3、示例(在java中使用JSON)
教程链接(json-smple1.1.1.jar文件) 链接:http://pan.baidu.com/s/1qXPbYHm 密码:v0f0 如何使用java编程语言编码和解码JSON 首先准备环境以 ...
- MVC中处理Json和JS中处理Json对象
MVC中处理Json和JS中处理Json对象 ASP.NET MVC 很好的封装了Json,本文介绍MVC中处理Json和JS中处理Json对象,并提供详细的示例代码供参考. MVC中已经很好的封装了 ...
- js中的json对象详细介绍
JSON一种简单的数据格式,比xml更轻巧,在JavaScript中处理JSON数据不需要任何特殊的API或工具包,下面为大家详细介绍下js中的json对象, 1.JSON(JavaScript Ob ...
- java中对集合对象list的几种循环访问
java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static void main(String[] args) { List<String> li ...
随机推荐
- rx tx
- PC-老鸟装机
老鸟装机 一.硬件安装莫疏忽 1>安装硬盘有讲究 1.单硬盘+单光驱 IDE1----硬盘(Mastet接口) IDE2----光驱(Mastet接口) ...
- A Tour of Go Nil slices
The zero value of a slice is nil. A nil slice has a length and capacity of 0. (To learn more about s ...
- Android:从程序员到架构师之路Ⅰ
一般而言,人们大多先学开发(代码)的技术,随后才学(架构)设计的方法.然而,在实际做事时,却是先设计,随后才写出代码来.敏捷过程则让设计与写码迭代循环下去,一直到完成为止.在本课程里,就遵循敏捷的迭代 ...
- 对css中的浮动属性float刨根解牛
1.浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 脱离常规流,由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样. 2.几张图说明浮动常 ...
- oracl使用DataBase Configuration Assistant创建、删除数据库
原文:oracl使用DataBase Configuration Assistant创建.删除数据库 可以使用DataBase Configuration Assistant来创建一个心得数据库.Da ...
- ASP.NET用HttpListener实现文件断点续传
本文转载:http://www.cnblogs.com/TianFang/archive/2007/01/03/610739.html 断点续传的原理很简单,就是在Http的请求和应答的报文头上和一般 ...
- hdu4486 Pen Counts(水题)
Pen Counts Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- C++ Combobox输入时自动完成
Combobox 在输入时自动完成 关键点 实现过程 BOOL m_bAuto; BOOL CProject02Dlg::PreTranslateMessage(MSG* pMsg) { i ...
- Ubuntu安装一:VM安装具体解释
1.下载VM中文版:http://download.pchome.net/system/sysenhance/down-4673-1.html,解压后双击安装包: 2.点击:下一步 3.允许安装协议, ...