当前位置: 代码迷 >> JavaScript >> java 转json 报差错理机制 案例
  详细解决方案

java 转json 报差错理机制 案例

热度:481   发布时间:2012-10-06 17:34:01.0
java 转json 报错处理机制 案例 .
JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。



报错处理机制

commons-beanutils-1.8.0.jar不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean

commons-collections.jar 不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap

commons-lang-2.4.jar不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException

commons-logging-1.1.1.jar不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

ezmorph-1.0.4.jar不加这个包
java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher

json-lib-2.3-jdk15.jar不加这个包
java.lang.NoClassDefFoundError: net/sf/json/JSONObject


我把这段代码拿到junit下测试 就报错了。java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException错误,查了下原因原来是少了3个包。因为我在使用json的时候只导入了json-lib-2.1.jar这个包 但是程序还需要这4个包:
commons-beanutils-1.7.0.jar

commons-collections-3.2.jar

commons-lang-2.1.jar
ezmorph-1.0.2.jar
导入之后就发现 OK了。


下载地址:http://json-lib.sourceforge.net/
还要需要的第3方包:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom



1、List
Java代码 
boolean[] boolArray = new boolean[]{true,false,true};     
           JSONArray jsonArray1 = JSONArray.fromObject( boolArray );     
           System.out.println( jsonArray1 );     
           // prints [true,false,true]    
             
           List list = new ArrayList();     
           list.add( "first" );     
           list.add( "second" );     
           JSONArray jsonArray2 = JSONArray.fromObject( list );     
           System.out.println( jsonArray2 );     
           // prints ["first","second"]    
 
           JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );     
           System.out.println( jsonArray3 );     
           // prints ["json","is","easy"]    

boolean[] boolArray = new boolean[]{true,false,true};  
            JSONArray jsonArray1 = JSONArray.fromObject( boolArray );  
            System.out.println( jsonArray1 );  
            // prints [true,false,true] 
           
            List list = new ArrayList();  
            list.add( "first" );  
            list.add( "second" );  
            JSONArray jsonArray2 = JSONArray.fromObject( list );  
            System.out.println( jsonArray2 );  
            // prints ["first","second"] 

            JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );  
            System.out.println( jsonArray3 );  
            // prints ["json","is","easy"]  

2、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 );     
         //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true} 

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 );  
         //{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}

3、BEAN
Java代码 
/** 
     * Bean.java  
        private String name = "json";    
        private int pojoId = 1;    
        private char[] options = new char[]{'a','f'};    
        private String func1 = "function(i){ return this.options[i]; }";    
        private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];"); 
    */   
JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );     
System.out.println( jsonObject );     
//{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}   

/**
     * Bean.java
        private String name = "json";  
        private int pojoId = 1;  
        private char[] options = new char[]{'a','f'};  
        private String func1 = "function(i){ return this.options[i]; }";  
        private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
    */
JSONObject jsonObject = JSONObject.fromObject( new JsonBean() );  
System.out.println( jsonObject );  
//{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }} 

4、BEANS
Java代码 
/** 
      * private int row ; 
          private int col ; 
          private String value ; 
      * 
      */ 
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);  
         System.out.println( ja.toString() );  
         //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}] 

/**
      * private int row ;
          private int col ;
          private String value ;
      *
      */
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);
         System.out.println( ja.toString() );
         //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]

5、String to bean
Java代码 
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";     
JSONObject jsonObject = JSONObject.fromString(json);     
Object bean = JSONObject.toBean( jsonObject );     
assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );     
  assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );     
  assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );     
   assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );     
   assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );     
  List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );     
  assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );   

String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";  
JSONObject jsonObject = JSONObject.fromString(json);  
Object bean = JSONObject.toBean( jsonObject );  
assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );  
  assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );  
  assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );  
   assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );  
   assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );  
  List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );  
  assertEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) ); 


Java代码 
String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";     
JSONObject jsonObject = JSONObject.fromString(json);  
  JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );     
   assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );     
     assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );     
     assertEquals( jsonObject.get( "value" ), bean.getValue() );   

String json = "{\"value\":\"xx\",\"row\":1,\"col\":1}";  
  JSONObject jsonObject = JSONObject.fromString(json);
   JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class );  
    assertEquals( jsonObject.get( "col" ),new Integer( bean.getCol())  );  
      assertEquals( jsonObject.get( "row" ), new Integer( bean.getRow() ) );  
      assertEquals( jsonObject.get( "value" ), bean.getValue() ); 


6  json to xml
1)
JSONObject json = new JSONObject( true ); 
String xml = XMLSerializer.write( json ); 

<o class="object" null="true"> 

2)
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}"); 
String xml = XMLSerializer.write( json ); 
<o class="object"> 
   <name type="string">json</name> 
   <bool type="boolean">true</bool> 
   <int type="number">1</int> 
</o> 
   <o class="object">
      <name type="string">json</name>
      <bool type="boolean">true</bool>
      <int type="number">1</int>
   </o>
3)
JSONArray json = JSONArray.fromObject("[1,2,3]"); 
String xml = XMLSerializer.write( json ); 
<a class="array">
   <e type="number">1</e> 
   <e type="number">2</e> 
   <e type="number">3</e> 
</a> 

7 、xml to json
<a class="array"> 
  <e type="function" params="i,j"> 
      return matrix[i][j]; 
  </e> 
</a> 
   <a class="array">
     <e type="function" params="i,j">
         return matrix[i][j];
     </e>
   </a>
  
JSONArray json = (JSONArray) XMLSerializer.read( xml ); 
System.out.println( json ); 
// prints [function(i,j){ return matrix[i][j]; }] 



在Java中加上这几个包

实例:

import java.io.IOException;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import aa.JsonBean2;


public class main extends HttpServlet {

 
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  List list = new ArrayList();
  JsonBean2 jb1 = new JsonBean2();
  jb1.setA(1);
  jb1.setB("丁龙飞");
  System.out.println("*****123");
  Date c=new Date(100,9,10);
  String s=c.toString();
  System.out.println(s+"33333");
  jb1.setC(s);
  JsonBean2 jb2 = new JsonBean2();
  jb2.setA(1);
  jb2.setB("b");
  jb2.setC(s);
  list.add(jb1);
  list.add(jb2);
  JSONObject obj= JSONObject.fromObject(jb1);
  System.out.println(obj.toString()+"*****123");
  JSONArray ja = JSONArray.fromObject(list);
  System.out.println(ja.toString()+"-------------");
  System.out.println("asdfasdf");
// List list=new ArrayList();
  String a="var a=[[1,{text:'ext'},{text:'q'},1,'132','321','2008-01-01 12:01:01'],[2,{text:'ext'},{text:'w'},2,'132','321','2008-01-01 12:01:01']]"; 
  response.getWriter().print(a);
 
}

 
public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

   doGet(request, response);
}

}
  相关解决方案