關於java中JSONArray、JSONObject、List、String之間的轉換


    本人最近幾個月才開始JAVA開發工作,以前一直做的BI開發,轉變的過程都是很艱難的。

   新產品開發的過程中,由於新人,能力有限,遇到了N多困難,幸虧有關系很好的同事在旁邊加油打氣。有好幾次我悄悄跟同事說,不行了,我堅持不下

去了,我就不是開發的料,同事每次都勸我開始都是難的,這段時間過去了你會發現其實沒那么難。是啊,就去登泰山,站在山腳下我就被它的巍峨嚇得望

而卻步,那我怎么可能會登頂看到日出。總嘚努力了才有放棄的資格。

   於是就這樣,堅持了幾個月,現在雖然自己依然是個菜鳥,但是進步還是很大。可能已經爬到了泰山的腳背上。

   開發過程中很讓人頭疼的就是各種類型之間的轉換,一開始真是蒙圈,后來用了幾次好點了,趕緊把自己記得的寫下來,怕以后忘了。

一、JASSONArray轉為JSONObject

       JSONArray result_type = new JSONArray();

       StringBuffer cdsIdxType = new StringBuffer();

       cdsIdxType.append(" select id from table_type ");

       result_type = jdbcTemp.queryForJSONArray(cdsIdxType.toString());

      JSONObject jsonObject = (JSONObject) result_type.get(i);

二、JASONArray轉為List

      JSONArray result_type = new JSONArray();


       StringBuffer cdsIdxType = new StringBuffer();


       cdsIdxType.append(" select id from table_type ");


       result_type = jdbcTemp.queryForJSONArray(cdsIdxType.toString());


       ArrayList list_type = new ArrayList();


       for (int i = 0; i < result_type.size(); i++) {


        JSONObject jsonObject = (JSONObject) result_type.get(i);


        list_type.add(jsonObject.get("id"));


       }

 

三、JSONArray轉為String

      

       JSONArray result_type = new JSONArray();


       StringBuffer cdsIdxType = new StringBuffer();


        cdsIdxType.append(" select id from table_type ");


       result_type = jdbcTemp.queryForJSONArray(cdsIdxType.toString());


       String typeAll = "";


       ArrayList list_type = new ArrayList();


       for (int i = 0; i < result_type.size(); i++) {


        JSONObject jsonObject = (JSONObject) result_type.get(i);


        list_type.add(jsonObject.get("id"));
       
       }


       for(int j=0;j<list_type.size();j++){


        typeAll = typeAll + "'" + list_type.get(j) + "'" + ",";


       }


       typeAll = typeAll.substring(0, typeAll.length() -1);

四、String轉換為ArrayList

     String tablecode = request.getParameter("tablecode");


      tablecode = tablecode.substring(1, tablecode.length()-1).replace("\"", "");


      String[] list = tablecode.split(",");


      ArrayList tables = new ArrayList();


      for(int i=0; i<list.length; i++){


       tables.add(list[i]);


      }

五.String轉JSONObject

String jsonMese = "{\"語文\":\"88\",\"數學\":\"78\",\"計算機\":\"99\"}";

JSONObject  myJson = JSONObject.fromObject(jsonMese);

六.String轉JSONArray

String jsonMessage = "[{'num':'成績', '外語':88, '歷史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," +           "{'num':'興趣', '外語':28, '歷史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," +           "{'num':'愛好', '外語':48, '歷史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]";  

JSONArray myJsonArray = JSONArray.fromObject(jsonMessage);  

七.String轉數組

String string = "a,b,c"; String [] stringArr= string.split(",");  //注意分隔符是需要轉譯

如果是"abc"這種字符串,就直接

String string = "abc" ; char [] stringArr = string.toCharArray(); //注意返回值是char數組 如果要返回byte數組就直接使用getBytes方法就ok了

String string = "abc" ; byte [] stringArr = string.getBytes();

八、數組轉String

char[] data={a,b,c}; 

String s=new String(data); 

注:本文是通過自己的總結和總結他人經驗得出來的結論,學疏才淺如果錯誤大家可以指出並改正。


     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM