读取JavaBean
1、一个JavaBean
Java代码:
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
StuBean stu = new StuBean();
stu.setId("s001");
stu.setName("Jahson");
stu.setPassword("123456");
JSONObject jsonObject = JSONObject.fromObject(stu);
out.println(jsonObject.toString());//将json放入页面
System.out.println(jsonObject);
out.flush();
out.close();
JQuery代码:
$.getJSON("servlet/MoreJson",function(data){
var str=status+"<br>";
str+="<table border=\"1\">";
str+="<tr>";
str+="<td>"+data.id+"</td>";
str+="<td>"+data.name+"</td>";
str+="<td>"+data.password+"</td>";
str+="</tr>";
str+="</table>";
$("#res").append(str);
});
2、多个JavaBean
Java代码:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
StuBean stu = new StuBean();
stu.setId("s001");
stu.setName("Jahson");
stu.setPassword("123456");
StuBean stu2 = new StuBean();
stu2.setId("s002");
stu2.setName("Jack");
stu2.setPassword("654321");
List list = new ArrayList();
list.add(stu);
list.add(stu2);
JSONArray jsonArray = JSONArray.fromObject(list);
out.println(jsonArray.toString());
System.out.println(jsonArray);
out.flush();
out.close();
JQuery代码:
$.post("servlet/MoreJson",{parma:v},function(data,status){
var str="<table border=\"1\">";
$.each(data,function(index,content){
str+="<tr>";
str+="<td>"+content.id+"</td>";
str+="<td>"+content.name+"</td>";
str+="<td>"+content.password+"</td>";
str+="</tr>";
});
str+="</table>";
$("#res").append(str);
},"json");
读取List
Java代码:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
List<String> list = new ArrayList<String>();
list.add("成都");
list.add("绵阳");
list.add("西昌");
list.add("aaa");
//如果是List,则必须use JSONArray instend
JSONArray jsonArray = JSONArray.fromObject(list);
out.println(jsonArray.toString());
out.flush();
out.close();
JQuery代码:
$.post("servlet/MoreJson",{parma:v},function(data,status){
var str="<table border=\"1\">";
$.each(data,function(index,content){
str+="<tr>";
str+="<td>"+content+"</td>";
str+="</tr>";
});
str+="</table>";
$("#res").append(str);
},"json");
结论:List通过each遍历数据信息;单独对象直接调用,多个对象放入LIst中通过each遍历
//从前台传入list对象至后台,并从后台返回list数据至前台
//前台代码
$("#saveIcon").click(function(){
var before = $("#hf_logo_img").val();
var after = $("#decoratContent_0").val();
if(before!=after){
var jsonData = '[{"decoratContent":"'+$("#decoratContent_0").val()+'","storeId":"'+$("#storeId_0").val()+'","locationId":"'+$("#locationId_0").val()+'"}]';
JEND.page.showLoading({title:'保存',loadingText:'正在保存内容…'});
$.post("storeDecoratLocation.do?method=saveLocation",
{jsonData:jsonData},
function(data,textStatus){
if(data){
$("#hf_banner_img").val($("#decoratContent_0").val());
$.each(data,function(index,content){
$("#seqId_0").val(content.seqId);
$("#status_0").val(content.status);
});
JEND.page.alert('保存成功');
}else{
JEND.page.alert('保存失败,请稍候重试');
}
},"json");
}
});
//后台代码
PrintWriter out = null;
String jsonData = request.getParameter("jsonData");
log.info("店铺装修jsonData:"+jsonData);
try{
out = response.getWriter();
JSONArray jr = JSONArray.fromObject(jsonData);
List<StoreDecoratLocation> list = jr.toList(jr, StoreDecoratLocation.class);
String operateUserId = (String)request.getSession().getAttribute(MerchantItemConstants.OPERATE_USER_ID_IN_SESSION);
String operateUserName = (String)request.getSession().getAttribute(MerchantItemConstants.OPERATE_USER_NAME_IN_SESSION);
if(list != null){
List<StoreDecoratLocation> sdlList = new ArrayList<StoreDecoratLocation>();
for (StoreDecoratLocation entity : list) {
System.out.println("storeId:=========="+ entity.getStoreId());
System.out.println("location=========="+entity.getLocationId());
System.out.println("decoratContent======"+entity.getDecoratContent());
entity.setStatus(StoreConstants.STORE_DECORAT_STATUS_MAKING);
entity.setSubmitUserId(Integer.parseInt(operateUserId));
entity.setSubmitUserName(operateUserName);
sdlList.add(entity);
}
//保存
MerchantStoreClient client = StoreTools.getMerchantStoreClient();
List<StoreDecoratLocation> saveStoreDecoratLocationList = client.saveOrUpdateStoreDecoratLocation(sdlList);
JSONArray jsonArray = JSONArray.fromObject(saveStoreDecoratLocationList);
out.println(jsonArray.toString());
System.out.println(jsonArray.toString());
out.flush();
out.close();
}
}catch(Exception ex){
ex.printStackTrace();
log.error(ex,ex);
out.print("failure");
}
return null;
//页面上遍历JSON数据
"name": "名字",
"age": "22",
"company": "lenovo"
};
for( var i in json){
alert(i + " " + json[i]);
}