tinguo002

           

          java FTP Linux系統下 文件上傳下載

               摘要: http://www.aygfsteel.com/parable-myth/archive/2008/05/16/200879.html Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->import java.io.Fil...  閱讀全文

          posted @ 2014-04-10 18:42 一堣而安 閱讀(758) | 評論 (0)編輯 收藏

          sun.net.ftp異常 PORT 192,168,0,80,9,205: 550 Permission denied.

          sun.net.ftp異常 PORT 192,168,0,80,9,205: 550 Permission denied.

          我使用sun.net.ftp來連接linux的vsftp時,出現如下的異常(在windows下是沒有的)

           

           

          java.io.FileNotFoundException: PORT 192,168,0,80,9,205: 550 Permission denied.

           

          at sun.net.ftp.FtpClient.readReply(FtpClient.java:236)

          at sun.net.ftp.FtpClient.issueCommand(FtpClient.java:193)

          at sun.net.ftp.FtpClient.openDataConnection(FtpClient.java:434)

          at sun.net.ftp.FtpClient.put(FtpClient.java:594)

          at org.lab24.util.FtpTool.processfile(FtpTool.java:193)

          at org.lab24.util.FtpTool.upload(FtpTool.java:116)

          at org.lab24.spider.Spider.run(Spider.java:55)

          at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:743)

          at java.lang.Thread.run(Thread.java:619)

          exception e in ftp upload(): java.io.FileNotFoundException: PORT 192,168,0,80,9,205: 550 Permission denied.

           

          后來查過網上的解決方案,才知道:

          該類默認使用的是Port模式傳輸數據,但實際上Linux配的是Pasv模式,所以傳不過去,修改辦法:

           

          ftpclient.sendServer("quote PASV");
          設置一下傳輸模式就可以傳了。

          posted @ 2014-04-10 14:19 一堣而安 閱讀(1527) | 評論 (0)編輯 收藏

          避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”異常

          /**解決辦法**
          * 1添加一個方法
          * 2調用這個方法生成一個新的httpClient對象
          **/



          import java.security.SecureRandom;
          import java.security.cert.CertificateException;
          import java.security.cert.X509Certificate;

          import javax.net.ssl.SSLContext;
          import javax.net.ssl.TrustManager;
          import javax.net.ssl.X509TrustManager;

          import org.apache.http.client.HttpClient;
          import org.apache.http.conn.ClientConnectionManager;
          import org.apache.http.conn.scheme.Scheme;
          import org.apache.http.conn.scheme.SchemeRegistry;
          import org.apache.http.conn.ssl.SSLSocketFactory;
          import org.apache.http.impl.client.DefaultHttpClient;

          public class HttpUtils
          {
              
          private HttpClient httpClient;
              
          public HttpUtils()
              
          {
                  httpClient 
          = null;
              }


              
          public void openConnection()
              
          {
                  httpClient 
          = new DefaultHttpClient();
                
            httpClient = getSecuredHttpClient(httpClient
              });
                  


              
              
          /**
               * 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”異常
               * 不用導入SSL證書
               * 
               * 
          @author shipengzhi(shipengzhi@sogou-inc.com)
               * 
               
          */

              
          private static DefaultHttpClient getSecuredHttpClient(HttpClient httpClient) {
                  
          final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
                  
          try {
                      SSLContext ctx 
          = SSLContext.getInstance("TLS");
                      X509TrustManager tm 
          = new X509TrustManager() {
                          @Override
                          
          public X509Certificate[] getAcceptedIssuers() {
                              
          return _AcceptedIssuers;
                          }


                          @Override
                          
          public void checkServerTrusted(X509Certificate[] chain,
                                  String authType) 
          throws CertificateException {
                          }


                          @Override
                          
          public void checkClientTrusted(X509Certificate[] chain,
                                  String authType) 
          throws CertificateException {
                          }

                      }
          ;
                      ctx.init(
          nullnew TrustManager[] { tm }new SecureRandom());
                      SSLSocketFactory ssf 
          = new SSLSocketFactory(ctx,
                              SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                      ClientConnectionManager ccm 
          = httpClient.getConnectionManager();
                      SchemeRegistry sr 
          = ccm.getSchemeRegistry();
                      sr.register(
          new Scheme("https"443, ssf));
                      
          return new DefaultHttpClient(ccm, httpClient.getParams());
                  }
           catch (Exception e) {
                      System.out.println(
          "=====:=====");
                      e.printStackTrace();
                  }

                  
          return null;
              }

          }

          posted @ 2014-04-09 17:34 一堣而安 閱讀(992) | 評論 (0)編輯 收藏

          string類型轉為double 值變化了


          http://zhidao.baidu.com/link?url=NeIRa5raPCZw7sR2DR1hzNk8aewMuNdDCNJ83qRMMWWfXaPVct7rWqVNWZnfkQAroEzRvKn5XxK4rK1lWEl2-_

          精度的問題!用基本類型的double類型進行運算可能會丟失精度。而且特別大的數又沒法處理。所以如果用BigDecimal這個類問題就解決了。這個類在java.Math包下。它可以處理任意精度的數據。對于樓主出現的問題,我從新寫了段代碼,供樓主參考。但是主要是還得查看API!代碼如下:

          import java.math.*;

          public class oopp

          {

           public static void 搜索main(String[] args)

           {

            String a="1467000000";

            double aa=Double.parseDouble(a);

            BigDecimal beichushu=new BigDecimal(aa);

            BigDecimal chushu=new BigDecimal(100000000);

            BigDecimal result=beichushu.divide(chushu,new MathContext(4));//MathConText(4)表示結果精確4位!

            boolean isTrue=String.valueOf(result).equals("14.67");

            System.out.println("1467000000除以100000000="+result);

            System.out.println(result+"與14.67比較的結果是"+isTrue);

           }

          }



          posted @ 2014-04-04 14:39 一堣而安 閱讀(732) | 評論 (0)編輯 收藏

          Tomcat啟動時一閃而過,看不到錯誤信息

          http://blog.163.com/kangle0925@126/blog/static/277581982011926102830712/

          有時Tomcat的啟動窗口一閃而過,根本就看不出啟動過程中發生了什么錯誤。這中間的原因有好多種,最常見的解決辦法就是使用run命令,打開startup.bat文件,找到下面這行:

          call "%EXECUTABLE%" start %CMD_LINE_ARGS%

          修改為:

          call "%EXECUTABLE%" run %CMD_LINE_ARGS%

          這樣,Tomcat啟動時就不會彈出新窗口,就可以從容不迫地觀察這的啟動錯誤了。

          修改文件后:需要從DOS命令行窗口進入到%TOMCAT_HOME%\bin路徑下,然后啟動startup.bat文件。

           

           Tomcat啟動時一閃而過,看不多錯誤信息 - Sky - Sky的博客

          posted @ 2014-04-04 14:21 一堣而安 閱讀(1765) | 評論 (0)編輯 收藏

          java日期

          http://blog.csdn.net/liwenfeng1022/article/details/6534176

          //字符串日期加1天
                 String sgrq = "20140101";
                 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                 try {
                   Date d = formatter.parse(sgrq);
                   String qxrq = formatter.format(d.getTime() + (1 * 24 * 60 * 60 * 1000));
                 } catch (ParseException e) {      
                   e.printStackTrace();
                 }


          1.用java.util.Calender來實現

             Calendar calendar=Calendar.getInstance();  
             calendar.setTime(new Date());
             System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天的日期
             calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//讓日期加1  
             System.out.println(calendar.get(Calendar.DATE));//加1之后的日期Top
          ===============================================================================

          2.用java.text.SimpleDateFormat和java.util.Date來實現
                    
              Date d=new Date();  
             SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
             System.out.println("今天的日期:"+df.format(d));  
             System.out.println("兩天前的日期:" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));  
             System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));

          ===============================================================================

          GregorianCalendar gc=new GregorianCalendar();
          gc.setTime(new Date);
          gc.add(field,value);
          value為正則往后,為負則往前
          field取1加1年,取2加半年,取3加一季度,取4加一周
          取5加一天....

          ===============================================================================

          /*
          *java中對日期的加減操作
          *gc.add(1,-1)表示年份減一.
          *gc.add(2,-1)表示月份減一.
          *gc.add(3.-1)表示周減一.
          *gc.add(5,-1)表示天減一.
          *以此類推應該可以精確的毫秒吧.沒有再試.大家可以試試.
          *GregorianCalendar類的add(int field,int amount)方法表示年月日加減.
          *field參數表示年,月.日等.
          *amount參數表示要加減的數量.
          *
          * UseDate.java 測試如下:
          */
          package temp.util;

          import java.util.Calendar;
          import java.util.Date;
          import java.util.GregorianCalendar;
          import java.text.SimpleDateFormat;
          public class UseDate {

          Date d=new Date();
          GregorianCalendar gc =new GregorianCalendar();
          SimpleDateFormat sf  =new SimpleDateFormat("yyyy-MM-dd");

          public String getYears()
          {
          gc.setTime(d);
          gc.add(1,+1);
          gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));

          return sf.format(gc.getTime());
          }

          public String getHalfYear()
          {
          gc.setTime(d);
          gc.add(2,+6);
          gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));

          return sf.format(gc.getTime());
          }
          public String getQuarters()
          {
          gc.setTime(d);
          gc.add(2,+3);
          gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));

          return sf.format(gc.getTime());
          }

          public String getLocalDate()
          {
          return sf.format(d);
          }


          public static  void  main(String[] args)
          {
          UseDate ud= new UseDate();
          System.out.println(ud.getLocalDate());
          System.out.println(ud.getYears());
          System.out.println(ud.getHalfYear());
          System.out.println(ud.getQuarters());
          }

          }
              //當月第一天和最后一天

               SimpleDateFormat sf  =new SimpleDateFormat("yyyyMMdd");
               Calendar a=Calendar.getInstance();
               int MaxDate=a.get(Calendar.DATE);
               System.out.println("該月最大天數:"+MaxDate+","+sf.format(a.getTime()));
               a.set(Calendar.DATE, 1);//把日期設置為當月第一天 
               System.out.println("當月第一天:"+sf.format(a.getTime())); 
               a.roll(Calendar.DATE, -1);//日期回滾一天,也就是最后一天

               System.out.println("當月最后一天:"+sf.format(a.getTime())); 

          4 ,

          GregorianCalendar gc=new GregorianCalendar();
                 
                  try {
                      gc.setTime( new SimpleDateFormat("yyyyMM").parse("200901"));
                      gc.add(2, -Integer.parseInt("7"));
                  } catch (ParseException e) {
                      // TODO 自動生成された catch ブロック
                      e.printStackTrace();
                  }
                  System.out.println(new SimpleDateFormat("yyyyMM").format(gc.getTime()));

          運行結果:200806

          posted @ 2014-03-18 21:01 一堣而安 閱讀(195) | 評論 (0)編輯 收藏

          jQuery on

          $(document).on("click","#flex1 tbody tr",function(){alert("The paragraph was clicked.");});

          posted @ 2014-03-15 17:00 一堣而安 閱讀(193) | 評論 (0)編輯 收藏

          JSON.stringify 語法實例講解

          詳細出處參考:http://www.jb51.net/article/29893.htm
          認識javascript也不短的時間了,可是這個用法說實在的,我還是第一次見過,慚愧啊慚愧啊。于是乎,在網上找了寫資料,寫了些例子 希望能給園子們一些幫助。 
          作用:這個函數的作用主要是為了系列化對象的。 
          可能有些人對系列化這個詞過敏,我的理解很簡單。就是說把原來是對象的類型轉換成字符串類型(或者更確切的說是json類型的)。就這么簡單。打個比方說,你有一個類,那么你可以通過這個方法轉換成相應的json類型的。很簡單吧。 
          接著看。 
          語法: 
          JSON.stringify(value [, replacer] [, space]) 
          value:是必須要的字段。就是你輸入的對象,比如數組啊,類啊等等。 
          replacer:這個是可選的。它又分為2種方式,一種是方法,第二種是數組。 
          情況一:我們先說數據,通過我們后面的實驗可以知道,它是和第一個有關系的。一般來說,我們系列化后的結果是通過鍵值對來進行表示的。 
          比如說: 
          name:"lan",age:25 
          這種形式。 
          所以,如果這種形式的話,如果第二個的值在第一個存在,那么的話就以第二個的值做key,第一個值為value進行表示,如果不存在,sorry,忽略。【是不是有點抽象,我也這么覺得,不過你等一下看實驗 就OK了。。呼呼。】 
          情況二:如果是方法,那很簡單,就是說把系列化后的每一個對象(記住 是每一個)傳進方法里面進行處理。 
          space:很好理解,用什么來做分隔符的。 
          1.如果省略的話,那么顯示出來的值 就沒有分隔符。直接輸出來 
          2.如果是一個數字的話,那么它就定義縮進幾個字符,當然 如果大于10 ,則最大值為10. 
          3.如果是一些轉義字符,比如“\t”,表示回車,那么它每行一個回車。 
          4.如果僅僅是字符串,OK,就在每行輸出值的時候把這些字符串附加上去就OK。當然,最大長度也是10個字符。 
          開始用實例說明; 
          1.只有一個參數的情況下: 
          復制代碼 代碼如下:
          var student = new Object(); 
          student.name = "Lanny"; 
          student.age = "25"; 
          student.location = "China"; 
          var json = JSON.stringify(student); 
          alert(student); 
          結果如下: 
          有些人可能會懷疑JSON.stringify的作用,OK。那假如,我們不要這個函數。代碼下面的樣子: 
          復制代碼 代碼如下:
          var student = new Object(); 
          student.name = "Lanny"; 
          student.age = "25"; 
          student.location = "China"; 
          // var json = JSON.stringify(student); 
          alert(student); 
           恭喜你 得到的結果是: 
          沒騙你吧,繼續。
          2.第二個參數存在,并且第二個參數還是function的時候 
          復制代碼 代碼如下:
          var students = new Array() ; 
          students[0] = "Lanny"; 
          students[1] = "dong"; 
          students[2] = "I love you"; 
          var json = JSON.stringify(students,switchUpper); 
          function switchUpper(key, value) { 
          return value.toString().toUpperCase(); 
          alert(json); 
                  //var json = JSON.stringify(students, function (key,value) { 
                  //return value.toString().toUpperCase(); 
                 //}); 
              上面的方法也可以換成下面的,2個都是一樣,只是寫法有那么一點點的不一樣而已。 
           得到結果如下: 
          3.第二個參數存在,并且第二個參數不是function,而是數組的時候。 
          3.1 【誤區】如果第一個參數是數組,第二個參數也是數組的話,只顯示第一個參數的值。 
          比如: 
          復制代碼 代碼如下:
          var students = new Array() ; 
          students[0] = "Lanny"; 
          students[1] = "dong"; 
          students[2] = "I love you"; 
          var stu = new Array(); 
          stu[0] = "1"; 
          stu[1] = "2"; 
          var json = JSON.stringify(students,stu); 
          alert(json); 
           sorry 得到的結果就是: 
          第二個被忽略了,只是第一個被系列化了。 
          3.2 如果第一個是對象(這里說的對象就像在C#里面可以進行new的),第二個是數組的。 
          那么如果第二個的value在第一個存在,那么的話就以第二個的值做key,第一個值為value進行表示 
          復制代碼 代碼如下:
          var student = new Object(); 
          student.qq = "5485891512"; 
          student.name = "Lanny"; 
          student.age = 25; 
          var stu = new Array(); 
          stu[0] = "qq"; 
          stu[1] = "age"; 
          stu[2] = "Hi";//這個student對象里不存在。 
          var json = JSON.stringify(student,stu); 
          alert(json); 
           得到的結果如下: 
          因為stu[2] = "Hi";這個Hi 在第一個找不到,所以就不進行顯示了。 
          4.第三個參數 
          4.1.如果省略的話,那么顯示出來的值 就沒有分隔符。直接輸出來 
          比如: 
          復制代碼 代碼如下:
          var student = new Object(); 
          student.qq = "5485891512"; 
          student.name = "Lanny"; 
          student.age = 25; 
          var stu = new Array(); 
          stu[0] = "qq"; 
          stu[1] = "age"; 
          stu[2] = "Hi"; 
          var json = JSON.stringify(student,stu); 
          alert(json); 
           輸出的就是: 
          4.2.如果是一個數字的話,那么它就定義縮進幾個字符,當然 如果大于10 ,則最大值為10. 
          復制代碼 代碼如下:
          var student = new Object(); 
          student.qq = "5485891512"; 
          student.name = "Lanny"; 
          student.age = 25; 
          var stu = new Array(); 
          stu[0] = "qq"; 
          stu[1] = "age"; 
          stu[2] = "Hi"; 
          var json = JSON.stringify(student,stu,100);//注意這里的100 
          alert(json); 
          那么得到的是: 
          空開來了10個字符。 
          4.3.如果是一些轉義字符,比如“\t”,表示回車,那么它每行一個回車。 
          也是一樣。 
          4.4.如果僅僅是字符串,OK,就在每行輸出值的時候把這些字符串附加上去就OK。當然,最大長度也是10個字符。 
          如果是var json = JSON.stringify(student,stu,“HaiKou”);// 
          就這樣吧 。good night。 
          詳細出處參考:http://www.jb51.net/article/29893.htm

          posted @ 2014-02-27 23:50 一堣而安 閱讀(243) | 評論 (0)編輯 收藏

          JSONObject put,accumulate,element的區別

          public Object put (Object key, Object value) 將value映射到key下。如果此JSONObject對象之前存在一個value在這個key下,當前的value會替換掉之前的value
          Associates the specified value with the specified key in this map(optional operation). If the map previously contained . a mapping for this key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true .))
          public JSONObject accumulate (String key, Object value) 累積value到這個key下。這個方法同element()方法類似,特殊的是,如果當前已經存在一個value在這個key下那么一個JSONArray將會存儲在這個key下來保存所有累積的value。如果已經存在一個JSONArray,那么當前的value就會添加到這個JSONArray中
          。相比之下replace方法會替代先前的value
          Accumulate values under a key. It is similar to the element method except that if there is already an object stored 
          under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a 
          JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.
          public JSONObject element (String key, Object value) 將鍵/值對放到這個JSONObject對象里面。如果當前value為空(null),那么如果這個key存在的話,這個key就會移除掉。如果這
          個key之前有value值,那么此方法會調用accumulate()方法。
          Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from the JSONObject if it is 
          present. If there is a previous value assigned to the key, it will call accumulate.

          轉載:http://ljhzzyx.blog.163.com/blog/static/3838031220126810430157/

          posted @ 2014-02-27 23:24 一堣而安 閱讀(1007) | 評論 (0)編輯 收藏

          JSONObject對象使用


          JSON net.sf.json

          1.JAR包簡介

          要使程序可以運行必須引入JSON-lib包,JSON-lib包同時依賴于以下的JAR包:

          1. commons-lang.jar
          2. commons-beanutils.jar
          3. commons-collections.jar
          4. commons-logging.jar 
          5. ezmorph.jar
          6. json-lib-2.2.2-jdk15.jar

          2.JSONObject對象使用

          JSON-lib包是一個beans,collections,maps,java arrays 和XML和JSON互相轉換的包。在本例中,我們將使用JSONObject類創建JSONObject對象,然后我們打印這些對象的值。為了使用 JSONObject對象,我們要引入"net.sf.json"包。為了給對象添加元素,我們要使用put()方法。

          2.1.實例1

           package jsontest;


          import net.sf.json.JSONArray;
          import net.sf.json.JSONObject;

          public class JSONObjectSample {

              // 創建JSONObject對象
              private static JSONObject createJSONObject() {
                  JSONObject jsonObject = new JSONObject();
                  jsonObject.put("username", "huangwuyi");
                  jsonObject.put("sex", "男");
                  jsonObject.put("QQ", "413425430");
                  jsonObject.put("Min.score", new Integer(99));
                  jsonObject.put("nickname", "夢中心境");
                  return jsonObject;
              }

              public static void main(String[] args) {
                  JSONObject jsonObject = JSONObjectSample.createJSONObject();//靜待方法,直接通過類名+方法調用
                  
          // 輸出jsonobject對象
                  System.out.println("jsonObject:" + jsonObject);

                  // 判讀輸出對象的類型
                  boolean isArray = jsonObject.isArray();
                  boolean isEmpty = jsonObject.isEmpty();
                  boolean isNullObject = jsonObject.isNullObject();
                  System.out.println("是否為數組:" + isArray + ", 是否為空:" + isEmpty
                          + ", isNullObject:" + isNullObject);

                  // 添加屬性,在jsonObject后面追加元素。
                  jsonObject.element("address", "福建省廈門市");
                  System.out.println("添加屬性后的對象:" + jsonObject);

                  // 返回一個JSONArray對象
                  JSONArray jsonArray = new JSONArray();
                  jsonArray.add(0, "this is a jsonArray value");
                  jsonArray.add(1, "another jsonArray value");
                  jsonObject.element("jsonArray", jsonArray);
                  //在jsonObject后面住家一個jsonArray
                  JSONArray array = jsonObject.getJSONArray("jsonArray");
                  System.out.println(jsonObject);
                  
                  
                  System.out.println("返回一個JSONArray對象:" + array);
                  // 添加JSONArray后的值
                  
          // {"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"夢中心境","address":"福建省廈門市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
                  System.out.println("結果=" + jsonObject);

                  // 根據key返回一個字符串
                  String username = jsonObject.getString("username");
                  System.out.println("username==>" + username);

                  // 把字符轉換為 JSONObject
                  String temp = jsonObject.toString();
                  JSONObject object = JSONObject.fromObject(temp);
                  // 轉換后根據Key返回值
                  System.out.println("qq=" + object.get("QQ"));

              }

          }

          輸出結果

          jsonObject:{"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"夢中心境"}
          是否為數組:false, 是否為空:false, isNullObject:false
          添加屬性后的對象:{"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"夢中心境","address":"福建省廈門市"}
          {"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"夢中心境","address":"福建省廈門市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
          返回一個JSONArray對象:["this is a jsonArray value","another jsonArray value"]
          結果={"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"夢中心境","address":"福建省廈門市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
          username==>huangwuyi
          qq=413425430

          2.2.實例2.

          package jsontest;

          import net.sf.json.JSONArray;
          import net.sf.json.JSONObject;

          public class JSONTest {
              public static void main(String args[])
              {
                  JSONObject jsonObj0  = new JSONObject();
                  JSONObject jsonObj  = new JSONObject();
                  JSONObject jsonObj2  = new JSONObject();
                  JSONObject jsonObj3  = new JSONObject();
                  JSONArray jsonArray = new JSONArray();
                  
                  //創建jsonObj0
                  jsonObj0.put("name0", "zhangsan");
                  jsonObj0.put("sex1", "female");
                  System.out.println("jsonObj0:"+jsonObj0);
                  
                  //創建jsonObj1
                  jsonObj.put("name", "xuwei");
                  jsonObj.put("sex", "male");
                  System.out.println("jsonObj:"+jsonObj);
              
                  //創建jsonObj2,包含兩個條目,條目內容分別為jsonObj0,jsonObj1
                  jsonObj2.put("item0", jsonObj0);
                  jsonObj2.put("item1", jsonObj);
                  System.out.println("jsonObj2:"+jsonObj2);
                  
                  //創建jsonObj3,只有一個條目,內容為jsonObj2
                  jsonObj3.element("j3", jsonObj2);
                  System.out.println("jsonObj3:"+jsonObj3);
              
                  //往JSONArray中添加JSONObject對象。發現JSONArray跟JSONObject的區別就是JSONArray比JSONObject多中括號[]
                  jsonArray.add(jsonObj);
                  System.out.println("jsonArray:"+jsonArray);
                  
                  JSONObject jsonObj4  = new JSONObject();
                  jsonObj4.element("weather", jsonArray);
                  System.out.println("jsonObj4:"+jsonObj4);
              }
          }
          輸出結果:
          jsonObj0:{"name0":"zhangsan","sex1":"female"}
          jsonObj:{"name":"xuwei","sex":"male"}
          jsonObj2:{"item0":{"name0":"zhangsan","sex1":"female"},"item1":{"name":"xuwei","sex":"male"}}
          jsonObj3:{"j3":{"item0":{"name0":"zhangsan","sex1":"female"},"item1":{"name":"xuwei","sex":"male"}}}
          jsonArray:[{"name":"xuwei","sex":"male"}]
          jsonObj4:{"weather":[{"name":"xuwei","sex":"male"}]}


          關于java bean的處理

          創建java對象:

          public class Address {
           private String road;
           private String streate;
           private String provience;
           private String no;
           public String getRoad() {
            return road;
           }
           public void setRoad(String road) {
            this.road = road;
           }
           public String getStreate() {
            return streate;
           }
           public void setStreate(String streate) {
            this.streate = streate;
           }
           public String getProvience() {
            return provience;
           }
           public void setProvience(String provience) {
            this.provience = provience;
           }
           public String getNo() {
            return no;
           }
           public void setNo(String no) {
            this.no = no;
           }
          }

          1.將json對象轉化為java對象

           JSONObject jsonObject = JSONObject.fromObject("{\"no\":\"104\",\"provience\":\"陜西\",\"road\":\"高新路\",\"streate\":\"\"}");
            Address Address  = (Address) JSONObject.toBean(jsonObject,Address.class);
            log.info(Address.getNo());
            log.info(Address.getStreate());
            log.info(Address.getProvience());
            log.info(Address.getRoad());

           

          2.將java對象轉化為json對象

             將java對象轉化為json對象:

              Address address = new Address();
              address.setNo("104");
              address.setProvience("陜西");
              address.setRoad("高新路");
              address.setStreate("");
              JSONArray json = JSONArray.fromObject(address);
              log.info(json.toString());

           

            將java對象list轉化為json對象:

            Address address = new Address();
            address.setNo("104");
            address.setProvience("陜西");
            address.setRoad("高新路");
            address.setStreate("");
            Address address2 = new Address();
            address2.setNo("105");
            address2.setProvience("陜西");
            address2.setRoad("未央路");
            address2.setStreate("張辦");
            List list = new ArrayList();
            list.add(address);
            list.add(address2);
            JSONArray json = JSONArray.fromObject(list);
            log.info(json.toString());

          3.JSONArray轉化為list

          JSONObject jsonObject = JSONObject.fromObject("{\"no\":\"104\",\"provience\":\"陜西\",\"road\":\"高新路\",\"streate\":\"\"}");
            JSONArray jsonArray = new JSONArray();
            jsonArray.add("{\"no\":\"104\",\"provience\":\"陜西\",\"road\":\"高新路\",\"streate\":\"\"}");
            jsonArray.add("{\"no\":\"104\",\"provience\":\"陜西\",\"road\":\"高新路\",\"streate\":\"123\"}");
            Object object = JSONArray.toList(jsonArray,Address.class);

          轉載:http://www.cnblogs.com/hitwtx/articles/2468633.html

          posted @ 2014-02-27 23:07 一堣而安 閱讀(9712) | 評論 (0)編輯 收藏

          僅列出標題
          共17頁: First 上一頁 2 3 4 5 6 7 8 9 10 下一頁 Last 

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 桂林市| 新宁县| 灵川县| 定边县| 奎屯市| 青浦区| 吐鲁番市| 丹棱县| 拉萨市| 延长县| 秦皇岛市| 大余县| 车致| 南安市| 大埔县| 广元市| 安西县| 辽中县| 逊克县| 当涂县| 九江市| 玉环县| 镇江市| 五大连池市| 广宗县| 屯门区| 三江| 三亚市| 九龙县| 芜湖市| 五常市| 沿河| 张家港市| 广饶县| 乐至县| 清徐县| 建德市| 微山县| 象州县| 井陉县| 安义县|