日期:2014-05-16  浏览次数:20403 次

java & JSON 转换
public class JsonBean2 {
	
	private int col;
	private int row;
	private String value;	

	public int getCol() {
		return col;
	}
	public void setCol(int col) {
		this.col = col;
	}
	public int getRow() {
		return row;
	}
	public void setRow(int row) {
		this.row = row;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}

}

?

???

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class Test {

	public static void main(String[] args) {
		boolean[] boolArray = new boolean[]{true,false,true};       
        JSONArray jsonArray1 = JSONArray.fromObject( boolArray );       
        System.out.println( jsonArray1 );   
        
        List list = new ArrayList();       
        list.add( "first" );       
        list.add( "second" );       
        JSONArray jsonArray2 = JSONArray.fromObject( list );       
        System.out.println( jsonArray2 );      
       

        JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );       
        System.out.println( jsonArray3 );     
        
        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 );       

        List list2 = 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("");    
           
           
        list2.add(jb1);    
        list2.add(jb2);    
           
        JSONArray ja = JSONArray.fromObject(list2);    
        System.out.println( ja.toString() );    

	}

}

?

??? 上面是代码,附近中是用到的jar包。(java环境:JDK1.5)

?