JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。本文将快速讲解 JSON 格式,并通过代码示例演示如何分别在客户端和服务器端进行 JSON 格式数据的处理。
使用JSON,在SERVLET或者STRUTS的ACTION中取得数据时,如果会出现异常:java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
是因为需要的类没有找到,一般,是因为少导入了JAR包,
出现java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher错误是因为没有导入ezmorph.jar文件或版本不对。
出现java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap错误是因为没有导入commons-collections.jar文件或版本不对。
使用JSON时,除了要导入JSON网站上面下载的json-lib-2.2-jdk15.jar包之外,还必须有其它几个依赖包:
commons-beanutils.jar,
commons-collections.jar ,
commons-httpclient.jar,
commons-lang.jar,
commons-logging-1.0.4.jar,
ezmorph.jar,
morph-1.0.1.jar
这几个包也是需要导入的.如果缺少里面的:ezmorph.jar包,则即出现上述异常
commons系列的包,可在网站:[url]http://www.docjar.com/[/url]上面搜索下载,其它包可下载网站如下:
[url]http://json-lib.sourceforge.net/[/url]
[url]http://sourceforge.net/projects/json-lib/files/[/url]
[url]http://ezmorph.sourceforge.net/[/url]
[url]http://morph.sourceforge.net/[/url]
import net.sf.json.*;
Java代码转换成json代码
1. List集合转换成json代码
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Map集合转换成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);
3. Bean转换成json代码
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 数组转换成json代码
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 一般数据转换成json代码
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
6. beans转换成json代码
List list = new ArrayList();
JsonBean2 jb1 = new JsonBean2();
jb1.setCol(1);
jb1.setRow(1);
jb1.setValue("xx");
JsonBean2 jb2 = new JsonBean2();
jb2.setCol(2);
jb2.setRow(2);
jb2.setValue("");
list.add(jb1);
list.add(jb2);
JSONArray ja = JSONArray.fromObject(list);
/*
* list 转换json
* */
public static String listToJsonString(List results){
JSONArray jsonArray= JSONArray.fromObject( results );
String str=" ";
for(int j=0;j<10;j++){
str+=jsonArray.getJSONObject(j).getString("Product_id")+" ";
}
return str ;
}
使用JSON,在SERVLET或者STRUTS的ACTION中取得数据时,如果会出现异常:java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
是因为需要的类没有找到,一般,是因为少导入了JAR包,
出现java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher错误是因为没有导入ezmorph.jar文件或版本不对。
出现java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap错误是因为没有导入commons-collections.jar文件或版本不对。
使用JSON时,除了要导入JSON网站上面下载的json-lib-2.2-jdk15.jar包之外,还必须有其它几个依赖包:
commons-beanutils.jar,
commons-collections.jar ,
commons-httpclient.jar,
commons-lang.jar,
commons-logging-1.0.4.jar,
ezmorph.jar,
morph-1.0.1.jar
这几个包也是需要导入的.如果缺少里面的:ezmorph.jar包,则即出现上述异常
commons系列的包,可在网站:[url]http://www.docjar.com/[/url]上面搜索下载,其它包可下载网站如下:
[url]http://json-lib.sourceforge.net/[/url]
[url]http://sourceforge.net/projects/json-lib/files/[/url]
[url]http://ezmorph.sourceforge.net/[/url]
[url]http://morph.sourceforge.net/[/url]
import net.sf.json.*;
Java代码转换成json代码
1. List集合转换成json代码
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Map集合转换成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);
3. Bean转换成json代码
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 数组转换成json代码
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 一般数据转换成json代码
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
6. beans转换成json代码
List list = new ArrayList();
JsonBean2 jb1 = new JsonBean2();
jb1.setCol(1);
jb1.setRow(1);
jb1.setValue("xx");
JsonBean2 jb2 = new JsonBean2();
jb2.setCol(2);
jb2.setRow(2);
jb2.setValue("");
list.add(jb1);
list.add(jb2);
JSONArray ja = JSONArray.fromObject(list);
/*
* list 转换json
* */
public static String listToJsonString(List results){
JSONArray jsonArray= JSONArray.fromObject( results );
String str=" ";
for(int j=0;j<10;j++){
str+=jsonArray.getJSONObject(j).getString("Product_id")+" ";
}
return str ;
}