list json
通过地址连接get传值到后台 new String(json.getString(0).getBytes("ISO-8859-1"), "UTF-8")
通过json组装对象ajax传到后台 new String(json.getString(0).getBytes("ISO-8859-1"), "GBK")
==========================json组装list传到前台
action:
//查询用途
public String searchUse(){
Enterprise en=(Enterprise) session.getAttribute("enterprise");
F1808 f=new F1808();
String type=request.getParameter("type");
if("0".equals(type)){
f.setC2c("0");
f.setC2p("1");
}
if("1".equals(type)){
f.setC2c("1");
f.setC2p("0");
}
f.setCompanyId(en.getCompanyId());
try {
f.execute();
} catch (Exception e) {
e.printStackTrace();
}
List useList=f.getFundUsageList();
JSONArray json = JSONArray.fromObject(useList);
System.err.println(json.toString());
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
out.printf(json.toString());
out.flush();
out.close();
return null;
}
web:
function checkPSK(d){
var data=toajax(d.value);
if(data!=""){
var jsonList=eval("("+data+")");
alert(jsonList);
// for(var i=0;i<jsonList.length;i++){
// for(var key in jsonList[i]){
// alert("key:"+key+",value:"+jsonList[i][key]);
// }
// }
for(var i=0;i<jsonList.length;i++){
alert(jsonList[i]["serialCode"]+jsonList[i]["usage"]);
}
}
}
function toajax(type){
var result=null;
$.ajax({
type: "POST",
url: "transfer/searchUse.shtml?type="+type,
dataType:"String",
async:false ,
success: function(data){
result=data;
},
error:function(){
result="";
}
});
return result;
}
==========================json组装list传到前台
action:
JSONArray json=null;
try{
json=JSONArray.fromObject(employee);//employee前台传来的JASON字符串
}catch(Exception e){
e.printStackTrace();
return null;
}
EmployeeEntity em=new EmployeeEntity();//建立一个EmployeeEntity实例 并从jason中填写数据到实例中
String handType = null;
try {
em.setE_id(new String(json.getString(0).getBytes("ISO-8859-1"), "GBK"));
em.setE_name(new String(json.getString(1).getBytes("ISO-8859-1"), "GBK"));
em.setE_card(new String(json.getString(2).getBytes("ISO-8859-1"), "GBK"));
em.setE_IdCard(new String(json.getString(3).getBytes("ISO-8859-1"), "GBK"));
em.setE_salary(new String(json.getString(4).getBytes("ISO-8859-1"), "GBK"));
em.setE_detail(new String(json.getString(5).getBytes("ISO-8859-1"), "GBK"));
handType=new String(json.getString(6).getBytes("ISO-8859-1"), "GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Enterprise en=(Enterprise) session.getAttribute("enterprise");
request.setAttribute("en", en);
String resultStr= "";
//添加或者修改并获得结果码
if(null!=handType&&handType.equals("isUpdate")){
resultStr=this.customer.updateEmployee(en, em);
}else{
resultStr=this.customer.addEmployee(en, em);
}
if(resultStr.equals("0000")){
out.print("[\"SUCESS\",\"SUCCESS\"]");
}else{
out.print("[\""+getStateMsg(resultStr).split(",")[0].toString()+"\",\"ERROR\"]");
}
out.flush();
out.close();
return null;
jsp:
//员工JSON对象
function Employee(id,name,card,idCard,salary,detail,type){
this.id=id;
this.idCard=idCard;
this.name=name;
this.card=card;
this.salary=salary;
this.detail=detail;
this.type=type;
}
var i_id=document.getElementById("e_id").value;
var i_idCard=document.getElementById("e_idCard").value;
var i_name=document.getElementById("e_name").value;
var i_card=document.getElementById("e_card").value;
var i_salary=document.getElementById("e_salary").value;
var i_detail=document.getElementById("e_detail").value;
var i_type=document.getElementById("e_type").value;
var reg = /\s/g; //去字符串所有空格
i_card = i_card.replace(reg, "");
var item=new Array(i_id,i_name,i_card,i_idCard,i_salary,i_detail,i_type);
var employee=JSON.stringify(item);
if($("#e_type").val()=="isUpdate"){
handleEmployee("customer/updateEmployee.shtml?isUpdate=1&employee="+employee,employee);
}if($("#e_type").val()=="isAdd"){
hiConfirm('确认添加该信息吗?',"提示:",function(r){
if(r){
handleEmployee("customer/addEmpoyee.shtml?employee="+employee,employee);
resetText();
}
});
}
function handleEmployee(iurl,employee){
$.ajax({
type:"POST",
url:iurl,
cache:false,
dataType:"json",
data:employee,
async:false ,
success: function(result){
if(result[1]=="SUCCESS"){
$("#resultSpan").attr("class","handle_success");
$("#resultSpan").html("操作成功...  ");
closeButton('refresh');
}else if(result[1]=="ERROR"){
$("#resultSpan").attr("class","handle_failure");
$("#resultSpan").html(result[0]+"  ");
}
$("#e_id").focus(function(){
$("#resultSpan").attr("class","");
$("#resultSpan").html(" ");
});
}
});
}