jsp-js-java:json数据传值
来源:互联网 发布:简单c语言程序代码 编辑:程序博客网 时间:2024/05/24 03:54
jsp:
<table class="posttable">
<colgroup>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
<col style="min-width: 70px; width: 10%;"/>
</colgroup>
<tbody>
<tr>
<th colspan="5">(1)</th>
<th class="centerth"><div id="option1_1" data-option-no="1" class="optionNormal"><b>1</b></div></th>
<th class="centerth"><div id="option1_2" data-option-no="1" class="optionNormal"><b>2</b></div></th>
<th class="centerth"><div id="option1_3" data-option-no="1" class="optionNormal"><b>3</b></div></th>
<th class="centerth"><div id="option1_4" data-option-no="1" class="optionNormal"><b>4</b></div></th>
<th class="centerth"><div id="option1_5" data-option-no="1" class="optionNormal"><b>5</b></div></th>
</tr>
<tr>
<th colspan="5">(2))</th>
<th class="centerth"><div id="option2_1" data-option-no="2" class="optionNormal"><b>1</b></div></th>
<th class="centerth"><div id="option2_2" data-option-no="2" class="optionNormal"><b>2</b></div></th>
<th class="centerth"><div id="option2_3" data-option-no="2" class="optionNormal"><b>3</b></div></th>
<th class="centerth"><div id="option2_4" data-option-no="2" class="optionNormal"><b>4</b></div></th>
<th class="centerth"><div id="option2_5" data-option-no="2" class="optionNormal"><b>5</b></div></th>
</tr>
<tr>
<th colspan="5">(3)</th>
<th class="centerth"><div id="option3_1" data-option-no="3" class="optionNormal"><b>1</b></div></th>
<th class="centerth"><div id="option3_2" data-option-no="3" class="optionNormal"><b>2</b></div></th>
<th class="centerth"><div id="option3_3" data-option-no="3" class="optionNormal"><b>3</b></div></th>
<th class="centerth"><div id="option3_4" data-option-no="3" class="optionNormal"><b>4</b></div></th>
<th class="centerth"><div id="option3_5" data-option-no="3" class="optionNormal"><b>5</b></div></th>
</tr>
</tbody>
</table>
js:
var optionDatas = getOptionData(); //optionDatas= [[object Object],[object Object],[object Object]]
param : {
optionDatas : util.Obj2str(optionDatas) // util.Obj2str(optionDatas) =[{"optionNo":1,"score":"3"},{"optionNo":2,"score":"1"},{"optionNo":3,"score":"4"}]
},
function getOptionData(){
var optionDatas = [];
$("th div[id^='option']").each(function(){
var optionData = {};
if($(this).hasClass("optionSelect")){
optionData['optionNo'] = $(this).data('optionNo');
var ids = this.id.split('_');
optionData['score'] = ids[1];
optionDatas.push(optionData);
}
});
return optionDatas;
}
该方法为Util.js的方法
/* 对象转成json/json数组 */
function Obj2str(o)
{
if (o == undefined)
{
return "";
}
var r = [];
if (typeof o == "string")
return "\"" + o.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
if (typeof o == "object")
{
if (!o.sort)
{
for ( var i in o)
r.push("\"" + i + "\":" + Obj2str(o[i]));
if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString))
{
r.push("toString:" + o.toString.toString());
}
r = "{" + r.join() + "}";
} else
{
for ( var i = 0; i < o.length; i++)
r.push(Obj2str(o[i]));
r = "[" + r.join() + "]";
}
return r;
}
return o.toString().replace(/\"\:/g, '":""');
}
java:
// String optionDatas传入后台为字符串类型 ='[{"optionNo":1,"score":"3"},{"optionNo":2,"score":"1"},{"optionNo":3,"score":"4"}]';
if (StringUtil.isNotEmpty(optionDatas)) {
List<EchOption> options = json2Obj(optionDatas);
}
/**
* json数组转成List
*
* @param jsonArrayStr
* @return
*/
private static List<EchOption> json2Obj(String jsonArrayStr) {
JsonConfig jsonConfig = new JsonConfig();
JSONArray jsonObjects = JSONArray.fromObject(jsonArrayStr, jsonConfig);
List<EchOption> resultList = new ArrayList<EchOption>();
for (int i = 0; i < jsonObjects.size(); i++) {
JSONObject jsonObj = (JSONObject) jsonObjects.get(i); // jsonObj ={"optionNo":1,"score":"3"}
EchOption option = (EchOption) JSONObject.toBean(jsonObj, EchOption.class); //一条表记录 private Integer EchOption.optionNo=1; private Integer EchOption.score=3;
. resultList.add(option);
}
return resultList;
}
- jsp-js-java:json数据传值
- struts2和jsp的数据传值
- Struts2.5 利用Ajax将json数据传值到JSP
- Struts2.5 利用Ajax将json数据传值到JSP
- java数据传参数与传值
- WEB开发之前后台JSON类型数据传值
- Unity数据传值技术
- Struts2 复选框 数据传值
- ajax post data数据传值分析
- ajax post data数据传值分析
- java实现Excel将数据传进数据库中
- 数据传参
- 添加数据传表格
- Bootstrap 模态框(Modal)插件数据传值
- Ajax异步 无刷新修改数据传值部分
- Bootstrap 模态框(Modal)插件数据传值
- Bootstrap 模态框(Modal)插件数据传值
- activity和fragment之间的数据传值思路
- 获取文件编码
- 子线程循环10次 主线程循环100次 然后如此反复50次
- 第六次C++实验
- Git检出项目--Idea中使用Git
- ElasticsearchCRUD使用(十九)【索引热身】
- jsp-js-java:json数据传值
- 92-ICMP 协议(基础)
- Android:高仿百度外卖、美团、淘点点二级联动效果!
- mysql emoji 乱码问题
- Activity 启动模式详解
- SimpleDateFormat 时间格式化规范
- uC/OS-II源码解析(os_mem.c)
- css3基础笔记03-2d和3d过渡
- 互联网“变得更安全了”只是一种错觉