json 과 자바 데이터 교환

11225 단어 자바jsonbeanxmlJ#
JSON 과 JAVA 데이터 의 변환
키워드: json 자바
JSON - lib 라 는 자바 패 키 지 는 bean, map, XML 을 JSON 으로 변환 하고 JSON 을 bean 과 DynaBean 으로 되 돌 릴 수 있 습 니 다.
다운로드 주소:
http://json-lib.sourceforge.net/
필요 한 제3자 가방:
org. apache. comons (3.2 이상 버 전)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
1、List
자바 코드
  • 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
    자바 코드
  • 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
    자바 코드
  • /**
  •       * 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
    자바 코드
  • /**
  •        * 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
    자바 코드
  • 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" ) );

    자바 코드
  • 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 ); 2) JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}"); String xml = XMLSerializer.write( json ); json true 1 json true 1 3) JSONArray json = JSONArray.fromObject("[1,2,3]"); String xml = XMLSerializer.write( json ); 1 2 3 7 、xml to json return matrix[i][j]; return matrix[i][j]; JSONArray json = (JSONArray) XMLSerializer.read( xml ); System.out.println( json ); // prints [function(i,j){ return matrix[i][j]; }]
    나오다

    좋은 웹페이지 즐겨찾기