业务:前台ajax传递json对象形式,json对象数组形式数据,传递后台,解析json数据
开发环境:eclipse4.5+mave3.3+mysql5.7+tomcat7+jdk1.7
开发技术:springmvc+spring+mybatis+js+jquery+jsp
jar包:
com.alibaba
fastjson
1.2.3
1.前台js封装json数据案例:
//修改模板广告数据(对象数组形式json数据)
var modifyChannelAdverArr = [];
//楼层广告数据
for(var i=0;i<$("#floorsConRight_ad_box .floorAdverId").length;i++){
if($($("#floorsConRight_ad_box .floorAdverId")[i]).html()!=""){
var channelAdverId = $($("#floorsConRight_ad_box .floorAdverId")[i]).html();
var adverName = $($("#floorsConRight_ad_box .floorAdverId")[i]).parent().find(".adverName").val();
var adverPath = $($("#floorsConRight_ad_box .floorAdverId")[i]).parent().find(".adverPath").val();
var adverHref = $($("#floorsConRight_ad_box .floorAdverId")[i]).parent().find(".imgLink").val();
var adverSort = $($("#floorsConRight_ad_box .floorAdverId")[i]).parent().find(".floorAdverSort").val();
modifyChannelAdverArr.push({"adverFlag":"1","floorId":channelStoreyId,"atId":"161","adverType":"151","temp3":"0","temp5":"0","channelAdverId":channelAdverId,"adverName":adverName,"adverPath":adverPath,"adverHref":adverHref,"adverSort":adverSort});
}
}
//对象形式json数据
var jsonData={"id":1,"tag":"稚优泉","productId":5534,"productName":"六色眼影"};
var data = {} ;
data["modifyChannelAdverArr"] = modifyChannelAdverArr;
data["jsonData"]=jsonData;
$.ajax({
type: "POST",
url: "serveTempStoreyAjax.htm",
data: JSON.stringify(data),
contentType : "application/json;charsetset=UTF-8",
dataType:"json",
cache: true,
async: false,
error: function (request) {
showTipAlert("连接错误!");
},
success: function (data) {
if (data == 1) {
//serveyouLike();
showTipAlert("操作成功!");
$("#cancelfloorsEdit").click();
} else {
showTipAlert("操作失败!");
}
}
});
2.后台接受参数:
@RequestMapping("/serveTempStoreyAjax")
@ResponseBody
public int serveTempStoreyAjax(HttpServletRequest request, HttpServletResponse response,@RequestBody JSONObject obj) {
int count=0;
try {
count=storeyService.operateTempStoreyAjax(request, obj);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("操作模板楼层失败"+e.getMessage(),e);
}
//返回结果1成功0失败
return count;
}
3.测试方法:
//测试
public static void main(String[] args) {
//json数据格式为对象形式:{}
String jsonData1="{\"modifyTempStoreArr\":"
+ "{\"floorId\":\"2\",\"goodsCatId\":\"5768\",\"channelStoreyId\":\"256\",\"storeyImg\":\"http://qjnanjing.oss-cn-shanghai.aliyuncs.com/qj_nanjing/1504176460708.jpg\",\"storeyImgHref\":\"www.baidu.com\",\"storeyName\":\"精品服饰\"}"
+ "}";
//json数据格式为对象数组形式: [{},{}]
String jsonData2="{\"modifyChannelAdverArr\":["
+ "{\"floorId\":\"256\",\"adverHref\":\"\",\"temp3\":\"0\",\"adverType\":\"151\",\"adverFlag\":\"2\",\"adverSort\":\"1\",\"adverPath\":\"http://qjnanjing.oss-cn-shanghai.aliyuncs.com/qj_nanjing/1492671908992.jpg\",\"adverName\":\"1\",\"atId\":\"161\",\"channelAdverId\":\"2517\",\"temp5\":\"0\"},"
+ "{\"floorId\":\"256\",\"adverHref\":\"\",\"temp3\":\"0\",\"adverType\":\"151\",\"adverFlag\":\"2\",\"adverSort\":\"2\",\"adverPath\":\"http://qjnanjing.oss-cn-shanghai.aliyuncs.com/qj_nanjing/1492671908992.jpg\",\"adverName\":\"2\",\"atId\":\"161\",\"channelAdverId\":\"2518\",\"temp5\":\"0\"}"
+ "]}";
//首先将json字符串转为为json对象
JSONObject json1 = JSON.parseObject(jsonData1);
JSONObject json2 = JSON.parseObject(jsonData2);
System.out.println("json1:"+json1.toJSONString());
System.out.println("json2:"+json2.toJSONString());
//然后通过key名称获得值
JSONObject object=json1.getJSONObject("modifyTempStoreArr");
JSONArray array2=json2.getJSONArray("modifyChannelAdverArr");
System.out.println("object:"+object.toJSONString());
System.out.println("array2:"+array2.toJSONString());
//解析json数据
//方法一:
String storeyName=object.getString("storeyName");
Long goodsCatId=object.getLong("goodsCatId");
String storeyImg=object.getString("storeyImg");
String storeyImgHref=object.getString("storeyImgHref");
Integer floorIds=object.getInteger("floorId");
Long channelStoreyId=object.getLong("channelStoreyId");
System.out.println("storeyName:"+storeyName);
System.out.println("goodsCatId:"+goodsCatId);
System.out.println("storeyImg:"+storeyImg);
System.out.println("storeyImgHref:"+storeyImgHref);
System.out.println("floorId:"+floorIds);
System.out.println("channelStoreyId:"+channelStoreyId);
for(int i=0;i channelAdverList = JSON.parseArray(array2.toJSONString(),ChannelAdver.class);
for(int i=0;i
上诉测试方法里面实体对象:
ChannelStorey:
package com.qianjiang.channel.bean;
import java.io.Serializable;
import java.util.Date;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
/**
* 实体类-频道楼层
*
*/
public class ChannelStorey implements Serializable{
private static final long serialVersionUID = 1094593262511040855L;
/** 频道楼层编号 */
private Long channelStoreyId;
/** 频道楼层名称 */
@NotNull
@Pattern(regexp = "[^''\\[\\]\\<\\>?\\\\!]+")
private String storeyName;
/** 频道ID */
private Long channelId;
/** 频道模板ID */
private Long tempId;
/** 频道楼层层数 */
private Integer floorId;
/** 频道楼层图片地址 */
@Pattern(regexp = "[^\\<\\>]+|()")
private String storeyImg;
/** 频道楼层图片链接地址 */
@Pattern(regexp = "[^''\\[\\]\\<\\>?\\\\!]+|()")
private String storeyImgHref;
/** 频道楼层SEO */
private String storeySeo = "";
/** 频道楼层商品分类ID */
private Long goodsCatId;
/** 是否显示楼层图片 0:不显示 1:显示 */
private String showImg;
/** 是否显示热销排行 0:不显示 1:显示 */
private String showHotRecommend;
/** 是否显示楼层广告 0:不显示 1:显示 */
private String showAdver;
/** 是否显示楼层标签 0:不显示 1:显示 */
private String showTag;
/** 是否显示楼层品牌 0:不显示 1:显示 */
private String showTrademark;
/** 是否显示楼层布告板 0:不显示 1:显示 */
private String showBillboard;
/** 是否显示楼层分类的子分类 0:不显示 1:显示 */
private String showChildCate;
/** 是否启用 0:不启用; 1:启用 */
private String userStatus;
/** 是否删除 0:未删除 1:已删除 */
private String delflag;
/** 创建人ID */
private Long createUserId;
/** 创建时间 */
private Date createDate;
/** 修改人ID */
private Long updateUserId;
/** 修改时间 */
private Date updateDate;
/** 备用字段1 第三方ID */
private String temp1;
/** 备用字段2 楼层右侧导航图片 */
@Pattern(regexp = "[^\\<\\>]+|()")
private String temp2;
/** 备用字段3 */
private String temp3;
/** 备用字段4 */
private String temp4;
/** 备用字段5 */
private String temp5;
public Long getChannelStoreyId() {
return channelStoreyId;
}
public void setChannelStoreyId(Long channelStoreyId) {
this.channelStoreyId = channelStoreyId;
}
public String getStoreyName() {
return storeyName;
}
public void setStoreyName(String storeyName) {
this.storeyName = storeyName;
}
public Long getChannelId() {
return channelId;
}
public void setChannelId(Long channelId) {
this.channelId = channelId;
}
public Long getTempId() {
return tempId;
}
public void setTempId(Long tempId) {
this.tempId = tempId;
}
public Integer getFloorId() {
return floorId;
}
public void setFloorId(Integer floorId) {
this.floorId = floorId;
}
public String getStoreyImg() {
return storeyImg;
}
public void setStoreyImg(String storeyImg) {
this.storeyImg = storeyImg;
}
public String getStoreyImgHref() {
return storeyImgHref;
}
public void setStoreyImgHref(String storeyImgHref) {
this.storeyImgHref = storeyImgHref;
}
public String getStoreySeo() {
return storeySeo;
}
public void setStoreySeo(String storeySeo) {
this.storeySeo = storeySeo;
}
public Long getGoodsCatId() {
return goodsCatId;
}
public void setGoodsCatId(Long goodsCatId) {
this.goodsCatId = goodsCatId;
}
public String getShowImg() {
return showImg;
}
public void setShowImg(String showImg) {
this.showImg = showImg;
}
public String getShowHotRecommend() {
return showHotRecommend;
}
public void setShowHotRecommend(String showHotRecommend) {
this.showHotRecommend = showHotRecommend;
}
public String getShowAdver() {
return showAdver;
}
public void setShowAdver(String showAdver) {
this.showAdver = showAdver;
}
public String getShowTag() {
return showTag;
}
public void setShowTag(String showTag) {
this.showTag = showTag;
}
public String getShowTrademark() {
return showTrademark;
}
public void setShowTrademark(String showTrademark) {
this.showTrademark = showTrademark;
}
public String getShowBillboard() {
return showBillboard;
}
public void setShowBillboard(String showBillboard) {
this.showBillboard = showBillboard;
}
public String getShowChildCate() {
return showChildCate;
}
public void setShowChildCate(String showChildCate) {
this.showChildCate = showChildCate;
}
public String getUserStatus() {
return userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
public String getDelflag() {
return delflag;
}
public void setDelflag(String delflag) {
this.delflag = delflag;
}
public Long getCreateUserId() {
return createUserId;
}
public void setCreateUserId(Long createUserId) {
this.createUserId = createUserId;
}
/**
* 时间
* @return Date
*/
public Date getCreateDate() {
if (this.createDate != null) {
return new Date(this.createDate.getTime());
} else {
return null;
}
}
/**
* 时间
* @param createDate
*/
public void setCreateDate(Date createDate) {
if (createDate != null) {
Date tEmp = (Date) createDate.clone();
if (tEmp != null) {
this.createDate = tEmp;
}
}
}
public Long getUpdateUserId() {
return updateUserId;
}
public void setUpdateUserId(Long updateUserId) {
this.updateUserId = updateUserId;
}
/**
* 时间
* @return Date
*/
public Date getUpdateDate() {
if (this.updateDate != null) {
return new Date(this.updateDate.getTime());
} else {
return null;
}
}
/**
* 时间
* @param updateDate
*/
public void setUpdateDate(Date updateDate) {
if (updateDate != null) {
Date tEmp = (Date) updateDate.clone();
if (tEmp != null) {
this.updateDate = tEmp;
}
}
}
public String getTemp1() {
return temp1;
}
public void setTemp1(String temp1) {
this.temp1 = temp1;
}
public String getTemp2() {
return temp2;
}
public void setTemp2(String temp2) {
this.temp2 = temp2;
}
public String getTemp3() {
return temp3;
}
public void setTemp3(String temp3) {
this.temp3 = temp3;
}
public String getTemp4() {
return temp4;
}
public void setTemp4(String temp4) {
this.temp4 = temp4;
}
public String getTemp5() {
return temp5;
}
public void setTemp5(String temp5) {
this.temp5 = temp5;
}
}
ChannelAdver:
package com.qianjiang.channel.bean;
import java.io.Serializable;
import java.util.Date;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
/**
* 实体类-频道广告
*
*/
public class ChannelAdver implements Serializable
{
/**
* 序列号
*/
private static final long serialVersionUID = -5581190573581904835L;
/*
* 频道广告ID
*/
private Long channelAdverId;
/**
* 频道ID
*/
private Long channelId;
/**
* 模板ID
*/
private Long tempId;
/**
* 楼层ID
*/
private Long floorId;
/** 楼层标签ID */
private Long floorTagId;
/** 广告标题 */
@NotNull
@Pattern(regexp = "[^''\\[\\]\\<\\>?\\\\!]+")
private String adverName;
/** 广告图片路径 */
@Pattern(regexp = "[^\\<\\>]+|()")
private String adverPath;
/** 广告链接 */
@Pattern(regexp = "[^''\\[\\]\\<\\>?\\\\!]+|()")
private String adverHref;
/**
* 广告分类-关联系统字典表的广告分类
* 157:轮播大图片 159:轮播小图片 161:首页图片
*/
private Long atId;
/** 广告排序 */
private Integer adverSort;
/**
* 广告类型-关联系统字典表的广告类型
* 151:频道广告
*/
private Long adverType;
/** 是否启用 */
private String userStatus;
/** 广告描述 */
@Pattern(regexp = "[^''\\[\\]\\<\\>?\\\\!]+|()")
private String des;
private String delflag;
private Long createUserId;
private Date createDate;
private Long updateUserId;
private Date updateDate;
/** 分类导航ID */
private String temp1;
/** 副标题 */
@Pattern(regexp = "[^''\\[\\]\\<\\>?\\\\!]+|()")
private String temp2;
/** 是否分类导航父框广告 */
private String temp3;
/** 第三方ID */
private String temp4;
private String temp5;
/** 广告总标题 */
private String adverTitle;
/** 频道广告标识1普通广告2轮播广告3品牌广告 */
private Integer adverFlag;
public Long getChannelAdverId() {
return channelAdverId;
}
public void setChannelAdverId(Long channelAdverId) {
this.channelAdverId = channelAdverId;
}
public Long getChannelId() {
return channelId;
}
public void setChannelId(Long channelId) {
this.channelId = channelId;
}
public Long getTempId() {
return tempId;
}
public void setTempId(Long tempId) {
this.tempId = tempId;
}
public Long getFloorId() {
return floorId;
}
public void setFloorId(Long floorId) {
this.floorId = floorId;
}
public String getAdverName() {
return adverName;
}
public void setAdverName(String adverName) {
this.adverName = adverName;
}
public String getAdverPath() {
return adverPath;
}
public void setAdverPath(String adverPath) {
this.adverPath = adverPath;
}
public String getAdverHref() {
return adverHref;
}
public void setAdverHref(String adverHref) {
this.adverHref = adverHref;
}
public Long getAtId() {
return atId;
}
public void setAtId(Long atId) {
this.atId = atId;
}
public Integer getAdverSort() {
return adverSort;
}
public void setAdverSort(Integer adverSort) {
this.adverSort = adverSort;
}
public Long getAdverType() {
return adverType;
}
public void setAdverType(Long adverType) {
this.adverType = adverType;
}
public String getUserStatus() {
return userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getDelflag() {
return delflag;
}
public void setDelflag(String delflag) {
this.delflag = delflag;
}
public Long getCreateUserId() {
return createUserId;
}
public void setCreateUserId(Long createUserId) {
this.createUserId = createUserId;
}
/**
* 时间
*
* @return Date
*/
public Date getCreateDate() {
if (this.createDate != null) {
return new Date(this.createDate.getTime());
} else {
return null;
}
}
/**
* 时间
*/
public void setCreateDate(Date createDate) {
if (createDate != null) {
Date tEmp = (Date) createDate.clone();
if (tEmp != null) {
this.createDate = tEmp;
}
}
}
public Long getUpdateUserId() {
return updateUserId;
}
public void setUpdateUserId(Long updateUserId) {
this.updateUserId = updateUserId;
}
/**
* 时间
*
* @return Date
*/
public Date getUpdateDate() {
if (this.updateDate != null) {
return new Date(this.updateDate.getTime());
} else {
return null;
}
}
/**
* 时间
*/
public void setUpdateDate(Date updateDate) {
if (updateDate != null) {
Date tEmp = (Date) updateDate.clone();
if (tEmp != null) {
this.updateDate = tEmp;
}
}
}
public String getTemp1() {
return temp1;
}
public void setTemp1(String temp1) {
this.temp1 = temp1;
}
public String getTemp2() {
return temp2;
}
public void setTemp2(String temp2) {
this.temp2 = temp2;
}
public String getTemp3() {
return temp3;
}
public void setTemp3(String temp3) {
this.temp3 = temp3;
}
public String getTemp4() {
return temp4;
}
public void setTemp4(String temp4) {
this.temp4 = temp4;
}
public String getTemp5() {
return temp5;
}
public void setTemp5(String temp5) {
this.temp5 = temp5;
}
public Long getFloorTagId() {
return floorTagId;
}
public void setFloorTagId(Long floorTagId) {
this.floorTagId = floorTagId;
}
public String getAdverTitle() {
return adverTitle;
}
public void setAdverTitle(String adverTitle) {
this.adverTitle = adverTitle;
}
public Integer getAdverFlag() {
return adverFlag;
}
public void setAdverFlag(Integer adverFlag) {
this.adverFlag = adverFlag;
}
}
备注:
1.在日常开发中我们经常需要将json字符串转换为json对象,测试方法中给出了两种方法解析json数据,看到方法二最为简单,方便,
2.前台封装的json数据的key值名称必须跟后台实体对象的属性名称一致,否则得到的值为null