shenang博客技術文檔


          理論不懂就實踐,實踐不會就學理論!

          posts - 35,comments - 55,trackbacks - 0
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          留言簿(3)

          隨筆分類

          隨筆檔案

          JAVA專業站點

          JAVA博客

          JAVA頻道

          書香

          牛X人

          視頻

          積分與排名

          • 積分 - 71827
          • 排名 - 770

          最新評論

          閱讀排行榜

          評論排行榜

             JSON進階()

          JSON簡介
          JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式易于人閱讀和編寫同時也易于機器解析和生成它基于, 的一個子集 JSON采用完全獨立于語言的文本格式,但是也使用了類似于C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)這些特性使JSON成為理想的數據交換語言。

          (詳細請看我的上一篇文章)
           

          JSONjava各種數據的交換

          在做以下步驟之前,請到這個地主下載json.js

          http://www.json.org/json.js

          一、          字符串 ---json對象

           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="使用這個script" />
          17     </body>
          18 </html>
          19 
           

          二、 Json對象--字符串

           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("對象長度:"+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="使用這個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的數據交互,有不全的或是不對的地方請高手指教!
          posted on 2009-04-24 09:22 重慶理工小子 閱讀(2868) 評論(0)  編輯  收藏 所屬分類: ajax編程

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 邛崃市| 富民县| 涿州市| 海口市| 防城港市| 大渡口区| 新晃| 南靖县| 奉节县| 和田县| 台山市| 新安县| 五华县| 十堰市| 无棣县| 安远县| 宿松县| 法库县| 集安市| 昌黎县| 中宁县| 洛浦县| 咸丰县| 东明县| 通化县| 林芝县| 松溪县| 台东市| 博爱县| 屏东县| 湖州市| 民勤县| 建德市| 南江县| 通榆县| 邹城市| 莎车县| 育儿| 乌审旗| 义马市| 韶关市|