JSON对象数组到Java的转换


json用来在客户端和服务器端之间传输数据很是简洁和方便,但是如果客户端传输的是对象数组的话,得做如下的处理:

  1. JSONArray testJSONArray=JSONArray.fromObject(testArrayStr);  
  2. Collection testArray=JSONArray.toCollection(testJSONArray);  
  3. if(testArray!=null && !testArray.isEmpty())  
  4. {  
  5.     Iterator it=testArray.iterator();  
  6.     while(it.hasNext())  
  7.     {  
  8.         JSONObject testJSONObj=JSONObject.fromObject(it.next());  
  9.         Test test=(Test) JSONObject.toBean(testJSONObj,Test.class);  
  10.         ...  
  11.     }  
  12. }  

JSON包下载 http://sourceforge.net/projects/json-lib/

相关内容