- 浏览: 833231 次
- 性别:
- 来自: 厦门
文章分类
- 全部博客 (363)
- 2010年3月 (3)
- 2010年4月 (3)
- Java (116)
- ExtJs (22)
- EJB3.0 (11)
- JQuery (28)
- SqlServer (5)
- Oracle (17)
- hibernate (21)
- struts2 (14)
- php (10)
- JavaScript (11)
- jbpm (6)
- spring (24)
- lucene (2)
- ibatis (7)
- C# (8)
- mysql (11)
- json (3)
- webservice (3)
- 设计模式 (1)
- jdbc (1)
- servlet (2)
- ice (6)
- 日常软件问题 (2)
- 生活 (2)
- iphone (1)
- rest (3)
- ruby (2)
- linux (13)
- quartz (1)
- poi (1)
- redis (13)
- memcached (4)
- nosql (2)
- tomcat调优 (1)
- 项目管理 (0)
最新评论
-
天使建站:
jquery里和数组相关的操作 附带具体的实例 ...
jquery对象数组 -
Cy0941:
$('#formId').form('submit',...) ...
easyui的form表单提交处理 -
shmily2038:
swifth 写道楼主,,你的命令写错啦,,[root@ser ...
centos直接yum安装nginx -
swifth:
楼主,,你的命令写错啦,,[root@server ~]# y ...
centos直接yum安装nginx -
随遇而安DXX:
...
REST
json-lib demo
JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。
到http://json-lib.sourceforge.net下载json-lib-1.1-jdk15.jar,运行json还需要的包有:
jakarta commons-lang 2.4.jar
jakarta commons-beanutils 1.7.0.jar
jakarta commons-collections 3.2.jar
jakarta commons-logging 1.1.1.jar
ezmorph 1.0.6.jar
可在http://www.docjar.com上搜索下载。
1. 将java数组和java集合转化成一个json数组
通过静态工厂方法JSONArray.fromObject()将一个java数组或集合转化成一个json数组对象JSONArray.
实例1:转化java数组
Java代码
String [] array = {"a","d","1","b"};
JSONArray jsonArray = JSONArray.fromObject(array);
System.out.println(jsonArray);
String [] array = {"a","d","1","b"};
JSONArray jsonArray = JSONArray.fromObject(array);
System.out.println(jsonArray); 输出结果:["a","d","1","b"]//注意他们的顺序与java数组中的顺序是一致的。
实例2:转化List对象
Java代码
String [] array = {"d","a","1","b"};
List list = new ArrayList();
list.add( array );
list.add( "second element");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
String [] array = {"d","a","1","b"};
List list = new ArrayList();
list.add( array );
list.add( "second element");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray); 输出结果:[["d","a","1","b"],"second element"]
实例3:转化字符串
Java代码
String test = "['json','is','easy']";
JSONArray jsonArray = JSONArray.fromObject( test );
System.out.println(jsonArray);
String test = "['json','is','easy']";
JSONArray jsonArray = JSONArray.fromObject( test );
System.out.println(jsonArray);
使用JSONArray.fromObject()转化字符串时,字符串必须符合JSONArray的格式。
2.将Map或javaBean转化成JSONObject
实例4:转化Map
Java代码
Map map = new HashMap();
map.put("name","json");
map.put("bool",Boolean.TRUE);
map.put("int",new Integer(1));
map.put("arr",new String[]{"a","b"});
map.put("func","function(i){return this.arr[i];}");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
Map map = new HashMap();
map.put("name","json");
map.put("bool",Boolean.TRUE);
map.put("int",new Integer(1));
map.put("arr",new String[]{"a","b"});
map.put("func","function(i){return this.arr[i];}");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json); 输出:[“name”: “json”, “bool”:true, “int”,1, “arr”:[ “a”, “b”], “func”:function(i){return this.arr[i];}]
实例5:转化javaBean
Java代码
public class TestBean {
private String id;
private String name;
private int age;
private String email;
public TestBean() {
}
public TestBean(String id, String name, int age, String email) {
super();
this.id = id;
this.name = name;
this.age = age;
this.email = email;
}
//以下为get、set方法
}
public class TestBean {
private String id;
private String name;
private int age;
private String email;
public TestBean() {
}
public TestBean(String id, String name, int age, String email) {
super();
this.id = id;
this.name = name;
this.age = age;
this.email = email;
}
//以下为get、set方法
} 输出:{"age":36,"email":"","id":"2","name":"xxhh"}
//这是一个json object,其实就是一个javascript对象。注意其中属性的顺序,是排过序的。
3.将JSONObject转化为javaBean对象
实例6:
Java代码
String myJson = "{'age':36,'email':'','id':'2','name':'testName'}";
JSONObject jsonObject1 = JSONObject.fromObject( myJson );
TestBean bean = (TestBean) JSONObject.toBean( jsonObject1, TestBean.class );
System.out.println("beanName:" + bean.getName());
String myJson = "{'age':36,'email':'','id':'2','name':'testName'}";
JSONObject jsonObject1 = JSONObject.fromObject( myJson );
TestBean bean = (TestBean) JSONObject.toBean( jsonObject1, TestBean.class );
System.out.println("beanName:" + bean.getName()); 输出:beanName:testName
4.通过 Servlet 或 MVC 框架输出 JSON 时,需要设置正确的 MIME 类型(application/json)和字符编码。假定服务器使用 UTF-8 编码,则可以使用以下代码输出编码后的 JSON 文本:
Java代码
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(jsonObject);
out.flush();
out.close();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(jsonObject);
out.flush();
out.close(); 5.在前台处理jsonobject:
Js代码
var json = {"customerGroupList":
[{"id":{"customerGroupId":1,"languageId":1},"lastModified":null,"name":"medium level","priceId":1},
{"id":{"customerGroupId":2,"languageId":1},"lastModified":null,"name":"medium level2","priceId":1},
{"id":{"customerGroupId":3,"languageId":1},"lastModified":null,"name":"level3","priceId":1}],
"searchResultList":[{"custAddress":"1 Way Street,NeverNever,,PostCode1","custFirstName":"Andy","custLastLogon":"19/01/2010","custLastName":"Admin","custlogons":25},
{"custAddress":"3 Way Street,NeverNever,,PostCode3","custFirstName":"Olly","custLastLogon":"18/12/2009","custLastName":"Order","custlogons":0},
{"custAddress":"1 Way Street,NeverNever,,12345","custFirstName":"John","custLastLogon":"18/12/2009","custLastName":"Doe","custlogons":0},
{"custAddress":"2 Way Street,NeverNever,,PostCode2","custFirstName":"Caty","custLastLogon":"18/12/2009","custLastName":"Catalog","custlogons":0}]};
var json = {"customerGroupList":
[{"id":{"customerGroupId":1,"languageId":1},"lastModified":null,"name":"medium level","priceId":1},
{"id":{"customerGroupId":2,"languageId":1},"lastModified":null,"name":"medium level2","priceId":1},
{"id":{"customerGroupId":3,"languageId":1},"lastModified":null,"name":"level3","priceId":1}],
"searchResultList":[{"custAddress":"1 Way Street,NeverNever,,PostCode1","custFirstName":"Andy","custLastLogon":"19/01/2010","custLastName":"Admin","custlogons":25},
{"custAddress":"3 Way Street,NeverNever,,PostCode3","custFirstName":"Olly","custLastLogon":"18/12/2009","custLastName":"Order","custlogons":0},
{"custAddress":"1 Way Street,NeverNever,,12345","custFirstName":"John","custLastLogon":"18/12/2009","custLastName":"Doe","custlogons":0},
{"custAddress":"2 Way Street,NeverNever,,PostCode2","custFirstName":"Caty","custLastLogon":"18/12/2009","custLastName":"Catalog","custlogons":0}]}; 这是一个非常复杂的jsonobject对象.在后台,它是将一个个CustomerGroup对象和CustomerSearchResult对象分别存入的ArrayList中,然后再以customerGroupList和searchResultList为key存入HashMap中,最后转化为jsonobject对象传递到前台.在前台进行处理时,你可以把它看成一个变通地js对象,然后使用属性访问来得到它对应的属性值.
如:
Js代码
//就可以得到一个js数组对象,即json对象的前面一部分.即用[]括隹的那个对象.
var custGroupList = json.customerGroupList;
//同样.它也是得到一个js数组对象
var array2 = json.searchResultList;
for(var i = 0 ; i < array2.length; i++){
//得到array中的每一个对象
var cust = array[i];
var result = "";
//得到每一个对象的属性及属性值,当然也可以使用Object.attributeName.这 种方式得到对象的属性值,根据个人喜好
for(var attrName in cust){
result = "attribute name = " + attrName + "and attribute value = " + cust[attrName];
}
}
JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。
到http://json-lib.sourceforge.net下载json-lib-1.1-jdk15.jar,运行json还需要的包有:
jakarta commons-lang 2.4.jar
jakarta commons-beanutils 1.7.0.jar
jakarta commons-collections 3.2.jar
jakarta commons-logging 1.1.1.jar
ezmorph 1.0.6.jar
可在http://www.docjar.com上搜索下载。
1. 将java数组和java集合转化成一个json数组
通过静态工厂方法JSONArray.fromObject()将一个java数组或集合转化成一个json数组对象JSONArray.
实例1:转化java数组
Java代码
String [] array = {"a","d","1","b"};
JSONArray jsonArray = JSONArray.fromObject(array);
System.out.println(jsonArray);
String [] array = {"a","d","1","b"};
JSONArray jsonArray = JSONArray.fromObject(array);
System.out.println(jsonArray); 输出结果:["a","d","1","b"]//注意他们的顺序与java数组中的顺序是一致的。
实例2:转化List对象
Java代码
String [] array = {"d","a","1","b"};
List list = new ArrayList();
list.add( array );
list.add( "second element");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
String [] array = {"d","a","1","b"};
List list = new ArrayList();
list.add( array );
list.add( "second element");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray); 输出结果:[["d","a","1","b"],"second element"]
实例3:转化字符串
Java代码
String test = "['json','is','easy']";
JSONArray jsonArray = JSONArray.fromObject( test );
System.out.println(jsonArray);
String test = "['json','is','easy']";
JSONArray jsonArray = JSONArray.fromObject( test );
System.out.println(jsonArray);
使用JSONArray.fromObject()转化字符串时,字符串必须符合JSONArray的格式。
2.将Map或javaBean转化成JSONObject
实例4:转化Map
Java代码
Map map = new HashMap();
map.put("name","json");
map.put("bool",Boolean.TRUE);
map.put("int",new Integer(1));
map.put("arr",new String[]{"a","b"});
map.put("func","function(i){return this.arr[i];}");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
Map map = new HashMap();
map.put("name","json");
map.put("bool",Boolean.TRUE);
map.put("int",new Integer(1));
map.put("arr",new String[]{"a","b"});
map.put("func","function(i){return this.arr[i];}");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json); 输出:[“name”: “json”, “bool”:true, “int”,1, “arr”:[ “a”, “b”], “func”:function(i){return this.arr[i];}]
实例5:转化javaBean
Java代码
public class TestBean {
private String id;
private String name;
private int age;
private String email;
public TestBean() {
}
public TestBean(String id, String name, int age, String email) {
super();
this.id = id;
this.name = name;
this.age = age;
this.email = email;
}
//以下为get、set方法
}
public class TestBean {
private String id;
private String name;
private int age;
private String email;
public TestBean() {
}
public TestBean(String id, String name, int age, String email) {
super();
this.id = id;
this.name = name;
this.age = age;
this.email = email;
}
//以下为get、set方法
} 输出:{"age":36,"email":"","id":"2","name":"xxhh"}
//这是一个json object,其实就是一个javascript对象。注意其中属性的顺序,是排过序的。
3.将JSONObject转化为javaBean对象
实例6:
Java代码
String myJson = "{'age':36,'email':'','id':'2','name':'testName'}";
JSONObject jsonObject1 = JSONObject.fromObject( myJson );
TestBean bean = (TestBean) JSONObject.toBean( jsonObject1, TestBean.class );
System.out.println("beanName:" + bean.getName());
String myJson = "{'age':36,'email':'','id':'2','name':'testName'}";
JSONObject jsonObject1 = JSONObject.fromObject( myJson );
TestBean bean = (TestBean) JSONObject.toBean( jsonObject1, TestBean.class );
System.out.println("beanName:" + bean.getName()); 输出:beanName:testName
4.通过 Servlet 或 MVC 框架输出 JSON 时,需要设置正确的 MIME 类型(application/json)和字符编码。假定服务器使用 UTF-8 编码,则可以使用以下代码输出编码后的 JSON 文本:
Java代码
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(jsonObject);
out.flush();
out.close();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(jsonObject);
out.flush();
out.close(); 5.在前台处理jsonobject:
Js代码
var json = {"customerGroupList":
[{"id":{"customerGroupId":1,"languageId":1},"lastModified":null,"name":"medium level","priceId":1},
{"id":{"customerGroupId":2,"languageId":1},"lastModified":null,"name":"medium level2","priceId":1},
{"id":{"customerGroupId":3,"languageId":1},"lastModified":null,"name":"level3","priceId":1}],
"searchResultList":[{"custAddress":"1 Way Street,NeverNever,,PostCode1","custFirstName":"Andy","custLastLogon":"19/01/2010","custLastName":"Admin","custlogons":25},
{"custAddress":"3 Way Street,NeverNever,,PostCode3","custFirstName":"Olly","custLastLogon":"18/12/2009","custLastName":"Order","custlogons":0},
{"custAddress":"1 Way Street,NeverNever,,12345","custFirstName":"John","custLastLogon":"18/12/2009","custLastName":"Doe","custlogons":0},
{"custAddress":"2 Way Street,NeverNever,,PostCode2","custFirstName":"Caty","custLastLogon":"18/12/2009","custLastName":"Catalog","custlogons":0}]};
var json = {"customerGroupList":
[{"id":{"customerGroupId":1,"languageId":1},"lastModified":null,"name":"medium level","priceId":1},
{"id":{"customerGroupId":2,"languageId":1},"lastModified":null,"name":"medium level2","priceId":1},
{"id":{"customerGroupId":3,"languageId":1},"lastModified":null,"name":"level3","priceId":1}],
"searchResultList":[{"custAddress":"1 Way Street,NeverNever,,PostCode1","custFirstName":"Andy","custLastLogon":"19/01/2010","custLastName":"Admin","custlogons":25},
{"custAddress":"3 Way Street,NeverNever,,PostCode3","custFirstName":"Olly","custLastLogon":"18/12/2009","custLastName":"Order","custlogons":0},
{"custAddress":"1 Way Street,NeverNever,,12345","custFirstName":"John","custLastLogon":"18/12/2009","custLastName":"Doe","custlogons":0},
{"custAddress":"2 Way Street,NeverNever,,PostCode2","custFirstName":"Caty","custLastLogon":"18/12/2009","custLastName":"Catalog","custlogons":0}]}; 这是一个非常复杂的jsonobject对象.在后台,它是将一个个CustomerGroup对象和CustomerSearchResult对象分别存入的ArrayList中,然后再以customerGroupList和searchResultList为key存入HashMap中,最后转化为jsonobject对象传递到前台.在前台进行处理时,你可以把它看成一个变通地js对象,然后使用属性访问来得到它对应的属性值.
如:
Js代码
//就可以得到一个js数组对象,即json对象的前面一部分.即用[]括隹的那个对象.
var custGroupList = json.customerGroupList;
//同样.它也是得到一个js数组对象
var array2 = json.searchResultList;
for(var i = 0 ; i < array2.length; i++){
//得到array中的每一个对象
var cust = array[i];
var result = "";
//得到每一个对象的属性及属性值,当然也可以使用Object.attributeName.这 种方式得到对象的属性值,根据个人喜好
for(var attrName in cust){
result = "attribute name = " + attrName + "and attribute value = " + cust[attrName];
}
}
发表评论
-
extjs和jquery各适用于什么场景
2013-01-23 09:49 2075比较点 extjs jquery 是 ... -
extjs和jquery各适用于什么场景
2013-01-21 11:14 14151.应该说粒度不同,extjs ... -
sqlite 数据类型 全面
2011-06-14 13:49 1293一般数据采用的固定的 ... -
extjs direct下载地址
2010-09-11 18:23 1179http://code.google.com/p/direct ... -
json返回数据,首先要做的事
2010-09-08 12:45 1165var json=eval("("+jso ... -
ExtJS 面向对象
2010-08-25 15:15 10051:支持命名空间 <script type=&qu ... -
ExtJS border 布局
2010-08-25 14:42 1992border(边界) 布局是指,把面板组件显示在 北 东 南 ... -
Ext.data-DataProxy/HttpProxy/MemoryProxy/ScriptTagProxy
2010-08-03 09:15 1179Ext.data-DataProxy/HttpProxy/Me ... -
extjs表单的提交
2010-06-24 17:01 12764//ExtJs 表单提交 form.getForm(). ... -
JSONLib解析json数组
2010-06-24 15:33 1736public class JsonHelper { /** ... -
Jsonlib
2010-06-24 13:13 1360使用JSON-LIB可以极大的简化JAVA对象转换成JSON对 ... -
以"对象"方式接收json数据.(解决乱码问题)
2010-06-24 10:36 5425客户端(传输数据无中文): Java代码 //序号控件失去 ... -
extjs的xtype
2010-06-24 09:06 1318基本组件: xtype Class 描述 button E ... -
EXTJS 查询数据,分页显示时传送自定义参数,添加控件的任何事件模板
2010-06-21 13:25 2213EXTJS 查询数据,分页显示时传送自定义参数,添加控件的任何 ... -
Extjs 分页查询的参数问题
2010-06-21 12:57 2415先按条件查询数据,再点下一页保留住查询条件,解决方案是将查询参 ... -
EXT中grid分页实现的完整例子---从前台到后台
2010-05-25 00:14 13717// -------------------- 定义grid- ... -
ExtJs的el和contentEl的区别
2010-05-19 16:46 34351、把<div id="div_exam&qu ... -
Ext gird分页学习
2010-04-23 13:33 1062var store = new Ext.data.Store( ... -
SModel了解
2010-04-21 09:35 15201、比较 jsonlib,Xstream,gson,smode ... -
xstream实现JAVA对象和XML的相互转化
2010-04-20 21:56 2516首页 http://xstream.codehaus.org ...
相关推荐
这个学习笔记主要围绕`json-lib`的使用方法和关键特性进行阐述。 首先,`json-lib`支持多种Java对象到JSON的转换,包括基本类型、数组、集合、Map以及自定义的Java类。例如,你可以通过以下方式将一个HashMap转换为...
3. JSON对象与JSON数组的创建:学习笔记可能介绍如何使用JSON-lib创建JSON对象和数组。例如,你可以通过`net.sf.json.JSONObject`和`net.sf.json.JSONArray`类来实现。 4. Java对象转换为JSON:JSON-lib提供了`...
在本文档《json 学习笔记_***.pdf》中,主要介绍了在Java开发中,如何使用三个常用的JSON处理工具:struts json,json_lib,jackson-mapper。同时,也提到了在使用这些工具时可能遇到的一些常见错误及其处理方法。 ...
这篇"JSON学习笔记"可能涵盖了以下几个关键知识点: 1. JSON的基本结构:JSON数据由键值对(key-value pairs)组成,键和值之间用冒号隔开,键值对之间用逗号分隔。键必须是字符串,而值可以是字符串、数字、布尔值...
Vue学习笔记 Vue是一个基于JavaScript的渐进式框架,用于构建用户界面。下面是关于Vue的学习笔记。 一、Vue基本知识 在Vue中,以点vue结尾的文件称为组件文件,包含三部分:template、script、style。template是...
在Java中,处理JSON数据需要引入相关的库,如`json-lib`,并且要确保所有依赖的JAR文件都已正确添加。在Java代码中,可以使用`JSONArray`和`JSONObject`类将Java对象转换为JSON字符串: 1. `ArrayList`转换为`...
### Girl项目学习笔记1 #### 一、Spring Boot项目初始化与Maven镜像更换 **1.1 更换IDEA内置Maven镜像至阿里云中央仓库** 在启动Spring Boot项目之前,首先需要确保开发环境配置正确。对于使用IntelliJ IDEA作为...
标题“src & lib.zip”指的是一个包含Java Web开发中源代码(src)和库文件(lib)的...通过阅读文档、参考示例项目、完成练习,以及理解和应用src和lib中的代码,学习者能够逐步掌握Java EE平台的关键技术和开发流程。
### Apache CXF 学习笔记知识点汇总 #### 一、CXF简介 ##### 1.1 CXF概述 - **背景介绍**:Apache CXF 是一个高性能、功能丰富的开源框架,用于构建和消费 Web 服务。它融合了 Celtix 和 XFire 两个开源项目的...
在Android开发中,JSON数据解析是一项重要的技能,它涉及到如何处理服务器端返回的数据以及如何在客户端展示这些数据。...通过不断实践和学习,开发者可以更好地利用JSON进行数据交换,提高应用的效率和用户体验。
- **lib**:包含所有必要的JAR文件。 - **src**:包含源代码。 #### 二、Struts2框架开发步骤 **1. 搭建开发环境**: - **获取框架JAR文件**: - **Struts2核心库**:struts2.0-core.jar - **第三方依赖**:...
在本篇Java中jqGrid的学习笔记整理中,我们将聚焦于jqGrid与后台Java服务的数据交互。jqGrid是一款强大的JavaScript库,用于创建动态、交互式的表格,而这里的进阶篇(二)将涉及到如何通过Java后端来提供数据支持。 ...
本篇学习笔记将深入讲解Cookie的工作原理以及在`urllib`库中的应用。 Cookie是一种小型文本文件,由服务器发送到用户的浏览器,并由浏览器保存。它的主要用途是识别用户身份、维持会话状态,比如当你登录某个网站后...
【Solr学习心得与总结笔记】 一、站内搜索的技术选型 站内搜索的实现方式多样,但针对不同需求,技术选型有所不同: 1. 使用Lucene:Lucene是Java开发的全文检索库,提供了底层的搜索算法,但直接使用Lucene需要...
3. **jqgrid学习笔记网搜和个人经验1.pdf**:作者通过网络搜集和自己的实践,整理出的学习笔记,包含了常见问题解决方案和实践经验分享,对于初学者极具参考价值。 **总结** jgGrid作为一个强大的JavaScript表格...
Android入门笔记 1. 开发环境配置 在开始Android开发之前,首先需要配置好开发环境。Eclipse是一款经典的集成开发环境(IDE),适用于Android开发。你可以从以下地址下载Eclipse: - Eclipse下载地址:此处应填写...
【描述】: "这份资料集合是关于个人学习AJAX的心得,虽然较为初级且未经过系统的整理,但包含了我学习过程中的笔记和实例,适合初学者入门。" 【标签】: "比较杂乱" 在这些文档和资源中,我们可以提取出以下几个...
一、概述 妹子工作时需要大量地查询火车车次至南京的信息,包括该车次到达站(南京站or...python+tkinter实现界面,详见之前的学习笔记://www.jb51.net/article/131059.htm 最终效果图: 二、实现 1.界面读取待查询