上代码:
markuplayer1.jsp:
function doTrClick(id,index){
var url = "<%=basePath%>markuplayer1.do?method=read";
var pars = "id=" + id;
var ajax = new Ajax.Request(
url,
{method:'post',parameters:pars,onComplete:checkreq}
);
}
function checkreq(request){
var info = request.responseText;
var info2 = eval('('+info+')');
var re = info2.msg;
if(re == "success"){
g_countryArray = info2.countryArr; //注意地方 sendJSON中填充的数据
openChild("<%=basePath%>master/markuplayer/markuplayer1Details.jsp",750,380);
}
}
Java页面处理:
String countryCodes = markuplayer1.getCountrycode();
JSONArray countriesArray = new JSONArray();
//set countrycodes to json array
if(countryCodes != null && !"".equals(countryCodes)) {
String[] countryArray = countryCodes.split(",");
if(countryArray.length > 0) {
List<Countries> countries = (List<Countries>)request.getSession().getServletContext().getAttribute(MyConstants.COUNTRIES_KEY);
for(String countrycode : countryArray) {
for(Countries country : countries) {
if(countrycode.trim().equalsIgnoreCase(country.getCountrycode().trim())) {
JSONObject json = new JSONObject();
json.put("code", country.getCountrycode().trim());
json.put("countrynameEn", country.getCountrynameEn());
json.put("countrynameCn", country.getCountrynameCn());
json.put("countrynameTw", country.getCountrynameTw());
countriesArray.add(json);
}
}
}
}
}
JSONUtils.sendJSON(response, "success",countriesArray);
关键部分:
public static void sendJSON(HttpServletResponse response,String message, JSONArray countrycontent) {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter outResponse = null;
try {
outResponse = response.getWriter();
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg", message);
jsonObject.put("countryArr", countrycontent);
outResponse.print(jsonObject.toString());
outResponse.flush();
} catch (IOException e) {
log.error("json utils exception :" + e);
} finally {
if(outResponse != null) {
outResponse.close();
}
}
}
这样jsp页面就可以接收到JSONArray类型的countryArr,至此结束!
扩展:
markuplayer1Detail.jsp使用JSONArray类型的数据:
var g_countryList = [] ;
wwindow.onload = changeToReservations;
function changeToReservations(){
g_countryList = window.parent.g_countryArray;
}
点Edit弹出一个页面并利用接收到的JSONArray类型来填充数据:
<span id="genCountryList" onmouseover='this.style.cursor="pointer";' onClick="genCountryListTable('idCountryList');">
Edit
</span>
var g_countryList = [];
function genCountryListTable( id ) {
var obj = document.getElementById(id);
if(obj) {
obj.style.display = '';
}
var sCountryTable = [];
sCountryTable[sCountryTable.length] = "<table style='width:100%;font-size: 11px;'>";
for (var i=0; i<g_countryList.length; i++) {
sCountryTable[sCountryTable.length] = '<tr id="tr_'+i+'" onmouseover="domouseOver(this);" onmouseout="domouseOut(this);">';
sCountryTable[sCountryTable.length] = '<td style="text-align:center;">' + g_countryList[i].code + '</td>';
sCountryTable[sCountryTable.length] = '<td>' + g_countryList[i].countrynameEn + '</td>';
sCountryTable[sCountryTable.length] = '<td>' + g_countryList[i].countrynameCn + '</td>';
sCountryTable[sCountryTable.length] = '<td>' + g_countryList[i].countrynameTw + '</td>';
sCountryTable[sCountryTable.length] = '<td><a onmouseover="this.style.cursor=\'pointer\'; this.style.fontWeight=\'normal\'; this.style.color=\'white\';" onmouseout="this.style.color=\'black\'; this.style.fontWeight=\'normal\';" onclick="removeCountryCode(\''+g_countryList[i].code+'\',\'' + i + '\');">Remove</a></td>';
sCountryTable[sCountryTable.length] = '</tr>';
}
sCountryTable.push('</table>');
$('idCountryListContent').innerHTML = sCountryTable.join("");
}
弹出窗口代码(省略CSS,关键):
<!-- hidden panel of details of country list -->
<div id="idCountryList" style="display: none;">
<div id="idCountryListHandle">
<a href="javascript:;" id="close" name="close" onClick="closePanel('idCountryList')">[ x ]</a>
List of selected country
</div>
<table style="border:0px solid red;width:100%;font-size: 11px;">
<tr style="font-weight:900;">
<td style="width:20%">Country Code</td>
<td style="width:20%">Country name (EN)</td>
<td style="width:20%">Country name (CN)</td>
<td style="width:20%">Country name (TW)</td>
<td style="width:20%">Operation</td>
</tr>
</table>
<div id="idCountryListContent"></div>
</div>
效果图:
- 大小: 26 KB
分享到:
相关推荐
Layui数据表格 前后端json数据接收的方法.docx
使用@RequestBody标签接收json数据,参数类型不能用Sting 看这两个注解的作用 @RequestParam 是作用在形参列表上,RequestParam可以接受简单类型的属性,也可以接受对象类型。在前端传入的是json字符串,后台按字符...
C#开发webservice接口,对客户端post服务的Json数据进行接收反馈 接收到的数据流转换成string类型,有其他需求对json解析,自己写个解析去查询下. 然后反馈json发送给请求端。
struts2返回json数据用query在页面接收
但是在使用过程中存在着一个问题,如果数据不存在需要判空的逻辑,这种判空逻辑会扩散在代码的各个角落,很容易犯错。 本文就是为了将这个变化点收缩到一处,便于控制。设计中也分别对JObject类和JArray类进行了...
主要介绍了Java实现后台发送及接收json数据的方法,结合实例形式分析了java针对json格式数据的传输与操作相关技巧,需要的朋友可以参考下
以上这篇python使用response.read()接收json数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。 您可能感兴趣的文章:Python简单读取json文件功能示例Python使用内置...
使用json数据生成表单页面
Js读取json数据实现滚动分页实例 Js读取json数据实现滚动分页实例 Js读取json数据实现滚动分页实例
Qt利用UDP通信来传输JSON数据以及接收并解析JSON数据,UDP Server端采用Qt Widgets技术,UDP Client端采用Qt Quick技术。
本代码借鉴了网上其他大神用C++实现的post方法,自己经过修改并添加了对接收的json数据流进行了解析的功能
ASP也能处理JSON数据?呵呵,刚才在Pjblog论坛上看到一个兄弟写的文章,没有测试,不过理论上一定是可以的~ 太晚了,不测试了。 以前处理JSON太麻烦了,输出还好说,循环一下就可以了,解析真的很头疼。所以遇到 这种...
php接收json数据,保存数据库
一个DataTable转化为Json数据代码的简单实现
微信小程序获取json页面数据
本代码实现的是通过数据库部分读到的json数据,填充到table表格中,并且也可以实现table表格中用户自行进行增删等操作,主要是jquery、js
本Java类实现了将标准的json字符串转换成map,并将数据解析到Excel的方法,本方法简单实用,可以批量导出数据到Excel文档中
json数据处理
前端展示json数据,格式化,可折叠展开