JSON的String字符串與Java的List列表對象的相互轉(zhuǎn)換
在前端:
1.如果json是List對象轉(zhuǎn)換的,可以直接遍歷json,讀取數(shù)據(jù)。
2.如果是需要把前端的List對象轉(zhuǎn)換為json傳到后臺,param是ajax的參數(shù),那么轉(zhuǎn)換如下所示:
var jsonStr = JSON.stringify(list); var param= {}; param.jsonStr=jsonStr;
在后臺:
1.把String轉(zhuǎn)換為List(str轉(zhuǎn)換為list)
List<T> list = new ArrayList<T>(); JSONArray jsonArray = JSONArray.fromObject(str);//把String轉(zhuǎn)換為json list = JSONArray.toList(jsonArray,t);//這里的t是Class<T>
2.把List轉(zhuǎn)換為json
JSONArray json = JSONArray.fromObject(object); String str = json.toString();//把json轉(zhuǎn)換為String
eg:
1. 根據(jù)頁面用戶輸入的信息形成 Answer 對象的List
/** * @param answers * @param question_ids * @param types * @return */ private List<Answer> toAnswerList(String[] studenAnswers, int[] question_ids, int[] types,int[] scores) { List<Answer> answerList = new ArrayList<Answer>(); if(studenAnswers!=null && question_ids!= null && types!= null&& scores!= null){ for (int i = 0; i < studenAnswers.length; i++) { Answer answer = new Answer(); String studenAnswer = studenAnswers[i]; int type = types[i]; int question_id = question_ids[i]; int score = scores[i]; answer.setQuestion_id(question_id); answer.setScore(score); answer.setStudenAnswer(studenAnswer); answer.setType(type); answerList.add(answer); } } return answerList; } /** * 將一個json字串轉(zhuǎn)為list * @param props * @return */ public static List<Answer> converAnswerFormString(String answer){ if (answer == null || answer.equals("")) return new ArrayList(); JSONArray jsonArray = JSONArray.fromObject(answer); List<Answer> list = (List) JSONArray.toCollection(jsonArray, Answer.class); return list; }
2. 將一個 Answer 對象的List 生成Json字串,是根據(jù)客戶端頁面用戶輸入的信息生成的
public String getAnswerString(String[] studenAnswers, int[] question_ids, int[] types,int[] scores) { List list = toAnswerList(studenAnswers, question_ids, types, scores); JSONArray jsonarray = JSONArray.fromObject(list); return jsonarray.toString(); }
PS:這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:
在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans
C語言風格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
相關(guān)文章
Java讀取txt文件中的數(shù)據(jù)賦給String變量方法
今天小編就為大家分享一篇Java讀取txt文件中的數(shù)據(jù)賦給String變量方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07springboot用戶數(shù)據(jù)修改的詳細實現(xiàn)
用戶管理功能作為所有的系統(tǒng)是必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于springboot用戶數(shù)據(jù)修改的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04在JPA中criteriabuilder使用or拼接多個like語句
這篇文章主要介紹了在JPA中criteriabuilder使用or拼接多個like語句,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Spring Boot項目維護全局json數(shù)據(jù)代碼實例
這篇文章主要介紹了Spring Boot項目維護全局json數(shù)據(jù)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02Spring源碼BeanFactoryPostProcessor詳解
BeanFactoryPostProcessor的執(zhí)行時機是在Spring掃描完成后,Bean初始化前,當我們實現(xiàn)BeanFactoryPostProcessor接口,可以在Bean的初始化之前對Bean進行屬性的修改,下面通過本文看下Spring源碼分析-BeanFactoryPostProcessor的實例代碼,感興趣的朋友一起看看吧2021-11-11