JSON进阶(一)
2010-09-14 13:44:19 来源:WEB开发网三、 List---json字符串
1 import java.util.ArrayList;
2 import java.util.List;
3 import net.sf.json.JSONArray;
4 public class test {
5 public static void main(String[] args) {
6
7 boolean[] boolArray = new boolean[]{true,false,true};
8 JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
9 System.out.println( jsonArray1 );
10 // prints [true,false,true]
11
12 List list = new ArrayList();
13 list.add( "first" );
14 list.add( "second" );
15 JSONArray jsonArray2 = JSONArray.fromObject( list );
16 System.out.println( jsonArray2 );
17 // prints ["first","second"]
18
19 JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );
20 System.out.println( jsonArray3 );
21 }
22 }
23
四、 Map ----json字符串
1 import java.util.HashMap;
2 import java.util.Map;
3 import net.sf.json.JSONObject;
4 public class test {
5 public static void main(String[] args) {
6 Map<String, Object> map = new HashMap();
7 map.put( "name", "json" );
8 map.put( "bool", Boolean.TRUE );
9
10 map.put( "int", new Integer(1) );
11 map.put( "arr", new String[]{"a","b"} );
12 map.put( "func", "function(i){ return this.arr[i]; }" );
13 JSONObject json = JSONObject.fromObject( map );
14 System.out.println(json);
15 }
16 }
17
更多精彩
赞助商链接