JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換
在前端:
1.如果json是List對(duì)象轉(zhuǎn)換的,可以直接遍歷json,讀取數(shù)據(jù)。
2.如果是需要把前端的List對(duì)象轉(zhuǎn)換為json傳到后臺(tái),param是ajax的參數(shù),那么轉(zhuǎn)換如下所示:
var jsonStr = JSON.stringify(list);
var param= {};
param.jsonStr=jsonStr;
在后臺(tái):
1.把String轉(zhuǎn)換為L(zhǎng)ist(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ù)頁(yè)面用戶(hù)輸入的信息形成 Answer 對(duì)象的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;
}
/**
* 將一個(gè)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. 將一個(gè) Answer 對(duì)象的List 生成Json字串,是根據(jù)客戶(hù)端頁(yè)面用戶(hù)輸入的信息生成的
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:這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
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語(yǔ)言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
相關(guān)文章
Java實(shí)現(xiàn)通過(guò)IP計(jì)算分析歸屬地實(shí)例分享
文章介紹了如何通過(guò)IP地址進(jìn)行歸屬地分析,包括IP地址的兩種類(lèi)型(IPV4和IPV6)以及計(jì)算歸屬地的方法,對(duì)于不精準(zhǔn)的需求,推薦使用開(kāi)源的字典庫(kù)如GeoIP2;對(duì)于高精度需求,可以購(gòu)買(mǎi)專(zhuān)業(yè)的IP網(wǎng)段數(shù)據(jù)并實(shí)時(shí)更新數(shù)據(jù)庫(kù),在設(shè)計(jì)項(xiàng)目時(shí)應(yīng)提前規(guī)劃數(shù)據(jù)結(jié)構(gòu),以避免數(shù)據(jù)清洗問(wèn)題2025-05-05
Java 8 對(duì) HashSet 元素進(jìn)行排序的操作方法
Java 中HashSet是一個(gè)不保證元素順序的集合類(lèi),其內(nèi)部是基于 HashMap 實(shí)現(xiàn)的,HashSet不支持排序,我們?cè)谛枰獙?duì)HashSet 排序時(shí),必須將其轉(zhuǎn)換為支持排序的集合或數(shù)據(jù)結(jié)構(gòu),如 List,本文將詳細(xì)介紹在 Java 8 中如何對(duì) HashSet 中的元素進(jìn)行排序,感興趣的朋友一起看看吧2024-11-11
Mybatis分頁(yè)插件PageHelper的使用詳解
這篇文章主要介紹了Mybatis分頁(yè)插件PageHelper的相關(guān)資料,該插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數(shù)據(jù)庫(kù)分頁(yè)需要的朋友可以參考下2016-12-12
Java讀取txt文件中的數(shù)據(jù)賦給String變量方法
今天小編就為大家分享一篇Java讀取txt文件中的數(shù)據(jù)賦給String變量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
springboot用戶(hù)數(shù)據(jù)修改的詳細(xì)實(shí)現(xiàn)
用戶(hù)管理功能作為所有的系統(tǒng)是必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于springboot用戶(hù)數(shù)據(jù)修改的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
在JPA中criteriabuilder使用or拼接多個(gè)like語(yǔ)句
這篇文章主要介紹了在JPA中criteriabuilder使用or拼接多個(gè)like語(yǔ)句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
JavaWeb實(shí)現(xiàn)文件上傳與下載的方法
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)文件上傳與下載的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01
Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例
這篇文章主要介紹了Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Spring源碼BeanFactoryPostProcessor詳解
BeanFactoryPostProcessor的執(zhí)行時(shí)機(jī)是在Spring掃描完成后,Bean初始化前,當(dāng)我們實(shí)現(xiàn)BeanFactoryPostProcessor接口,可以在Bean的初始化之前對(duì)Bean進(jìn)行屬性的修改,下面通過(guò)本文看下Spring源碼分析-BeanFactoryPostProcessor的實(shí)例代碼,感興趣的朋友一起看看吧2021-11-11

