java List與json的轉換

來源:互聯網
上載者:User
Java代碼  
  1. /** 
  2.      * 根據頁面使用者輸入的資訊形成 Answer 對象的List 
  3.      *  
  4.      * @param answers 
  5.      * @param question_ids 
  6.      * @param types 
  7.      * @return 
  8.      */  
  9.     private List<Answer> toAnswerList(String[] studenAnswers, int[] question_ids,  
  10.             int[] types,int[] scores) {  
  11.         List<Answer> answerList = new ArrayList<Answer>();  
  12.           
  13.         if(studenAnswers!=null && question_ids!= null && types!= null&& scores!= null){  
  14.             for (int i = 0; i < studenAnswers.length; i++) {  
  15.                   
  16.                 Answer answer = new Answer();  
  17.                 String studenAnswer = studenAnswers[i];  
  18.                 int type = types[i];  
  19.                 int question_id = question_ids[i];  
  20.                 int score = scores[i];  
  21.           
  22.                   
  23.                 answer.setQuestion_id(question_id);  
  24.                 answer.setScore(score);  
  25.                 answer.setStudenAnswer(studenAnswer);  
  26.                 answer.setType(type);  
  27.                   
  28.                 answerList.add(answer);  
  29.             }  
  30.         }  
  31.         return answerList;  
  32.     }  
  33.   
  34.     /** 
  35.      * 將一個json字串轉為list 
  36.      * @param props 
  37.      * @return 
  38.      */  
  39.     public static List<Answer> converAnswerFormString(String answer){  
  40.         if (answer == null || answer.equals(""))  
  41.             return new ArrayList();  
  42.   
  43.         JSONArray jsonArray = JSONArray.fromObject(answer);  
  44.         List<Answer> list = (List) JSONArray.toCollection(jsonArray,  
  45.                 Answer.class);  
  46.           
  47.         return list;  
  48.     }  
  49.       
  50.     /** 
  51.      *  
  52.      * 將一個 Answer 對象的List 產生Json字串 
  53.      * 是根據用戶端頁面使用者輸入的資訊產生的 
  54.      *  
  55.      *  
  56.      */  
  57.     public String getAnswerString(String[] studenAnswers, int[] question_ids,  
  58.             int[] types,int[] scores) {  
  59.         List list = toAnswerList(studenAnswers,  question_ids,  
  60.                  types, scores);  
  61.         JSONArray jsonarray = JSONArray.fromObject(list);  
  62.   
  63.         return jsonarray.toString();  
  64.     }  
  65.       
  66.       
  67.       
  68.       
  69.      

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.