json lib 学习笔记

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];  
    }  
}

你可能感兴趣的:(JavaScript,mvc,json,bean,框架)