JSON的String字符串与Java的List列表对象的相互转换
来源:互联网 发布:博士论文数据造假 编辑:程序博客网 时间:2024/06/05 05:13
原文链接:http://www.jb51.net/article/83177.htm
在前端:
1.如果json是List对象转换的,可以直接遍历json,读取数据。
2.如果是需要把前端的List对象转换为json传到后台,param是ajax的参数,那么转换如下所示:
var jsonStr = JSON.stringify(list); var param= {}; param.jsonStr=jsonStr;
在后台:
1.把String转换为List(str转换为list)
List<T> list = new ArrayList<T>(); JSONArray jsonArray = JSONArray.fromObject(str);//把String转换为json list = JSONArray.toList(jsonArray,t);//这里的t是Class<T>
2.把List转换为json
JSONArray json = JSONArray.fromObject(object); String str = json.toString();//把json转换为String
eg:
1. 根据页面用户输入的信息形成 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字串转为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字串,是根据客户端页面用户输入的信息生成的
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互相转换工具:
http://tools.jb51.net/code/xmljson
json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat
在线json压缩/转义工具:
http://tools.jb51.net/code/json_yasuo_trans
C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
阅读全文
0 0
- JSON的String字符串与Java的List列表对象的相互转换
- json对象与字符串...的相互转换
- Java中Json字符串与对象之间的相互转换
- Array(数组)与Json String (Json字符串) 的相互转换
- JSON与java对象的相互转换
- Java对象与Json的相互转换
- 非标准json,标准json,json字符串与js对象,Java对象之间的相互转换
- json格式字符串与json对象相互转换的函数
- Json字符串与Json对象的相互转换
- 补充:json对象与json字符串之间的相互转换
- json字符串与json对象的相互转换
- json字符串与json对象的相互转换
- json对象与json字符串的相互转换
- json字符串与json对象的相互转换
- json对象与json字符串之间的相互转换
- Json对象与Json字符串的相互转换
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
- 【JSON】json与List的相互转换
- URL的井号
- 【jzoj5389】【NOIP2017提高A组模拟9.26】【解梦】
- Raft 一致性算法论文译文
- Matlab 读写yuv文件
- js实现音频歌词同步
- JSON的String字符串与Java的List列表对象的相互转换
- 求正则覆盖&判断保持依赖
- [译] Kotlin 中我讨厌什么
- hihocoder#1345 : 小h的朋友们(矩阵快速幂)
- JSP基础知识(JSTL)
- org.hibernate.exception.com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
- Rotate List
- 接口----定义
- 整理第一个python爬虫的思路