JSON進(jìn)階(一)
JSON簡(jiǎn)介
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式易于人閱讀和編寫(xiě)同時(shí)也易于機(jī)器解析和生成它基于, 的一個(gè)子集 JSON采用完全獨(dú)立于語(yǔ)言的文本格式,但是也使用了類(lèi)似于C語(yǔ)言家族的習(xí)慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)這些特性使JSON成為理想的數(shù)據(jù)交換語(yǔ)言。
(詳細(xì)請(qǐng)看我的上一篇文章)
JSON與java各種數(shù)據(jù)的交換
在做以下步驟之前,請(qǐng)到這個(gè)地主下載json.js
http://www.json.org/json.js
一、 字符串 ---json對(duì)象
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <head>
5 <title>My JSP '1.jsp' starting page</title>
6 <script type="text/javascript" src="js/json.js"></script>
7 </head>
8 <body>
9 <script type="text/javascript">
10 function myEval() {
11 var str = '{ "name": "Violet", "occupation": "character" }';
12 var obj = eval('(' + str + ')');
13 alert(obj.name);
14 }
15 </script>
16 <input type="button" onclick="myEval()" value="使用這個(gè)script" />
17 </body>
18 </html>
19
二、 Json對(duì)象--字符串
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <head>
5 <title>My JSP '1.jsp' starting page</title>
6 <script type="text/javascript" src="js/json.js"></script>
7 </head>
8 <body>
9 <script type="text/javascript">
10 function test()
11 {
12 var myJSONtext =
13 {
14 "bindings":
15 [
16 {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
17 {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
18 {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
19 ]
20 };
21 var myObject = eval(myJSONtext);
22 alert("對(duì)象長(zhǎng)度:"+myObject.bindings.length);
23 for(var i=0;i<myObject.bindings.length;i++){
24 alert(myObject.bindings[i].method);
25 }
26 }
27 </script>
28 <input type="button" onclick="test()" value="使用這個(gè)script" />
29 </body>
30 </html>
31
三、 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
五、 Javabeans ---json字符串
public class JSONBean


{
private int row ;
private int col ;
private String value ;

public int getRow()
{
return row;
}

public void setRow(int row)
{
this.row = row;
}

public int getCol()
{
return col;
}

public void setCol(int col)
{
this.col = col;
}

public String getValue()
{
return value;
}

public void setValue(String value)
{
this.value = value;
}
}

java代碼:
1 List<JSONBean> l=new ArrayList<JSONBean>();
2 JSONBean jb=new JSONBean();
3 jb.setCol(1);
4 jb.setRow(1);
5 jb.setValue("huxl");
6
7 JSONBean jb2=new JSONBean();
8 jb2.setCol(2);
9 jb2.setRow(2);
10 jb2.setValue("ryp");
11
12 l.add(jb);
13 l.add(jb2);
14
15 JSONArray ja = JSONArray.fromObject(l);
16 System.out.println( ja.toString() );
17
以上就是JSON與java的數(shù)據(jù)交互,有不全的或是不對(duì)的地方請(qǐng)高手指教!
posted on 2009-04-24 09:22
重慶理工小子 閱讀(2862)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
ajax編程