json
- 相关基础知识
js发送json串
- json2.js
- 列表内容
- 相关代码
$(".sun_onclick").click(function () {
var pcategory = $(this).attr("pcategory");
var orderBy = $("select[name='orderBy']").val();
var maxMin = $("#amount").val();
var split = maxMin.split(/[ \t\n\x0B\f\r]+/);
var min = split[0].slice(1, split[0].length);
var max = split[1].slice(1, split[1].length);
var jsonDate = {
"pcategory": pcategory,
"orderBy": orderBy,
"min": min,
"max": max
}
$.ajax({
type: "POST",
url: "${app}/FindprodServlet",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(jsonDate), //json2中将js对象转换为json串
success: function (date) {
$(".men_clothing_tittle h2").text(date[0].pcategorys)
productAdd($("#sun_e"),date);
}
});
})
JSON 接收
导入jar
- jackson-mapper-asl-1.9.13.jar
- jackson-core-asl-1.9.13.jar
servlet中相关代码
BufferedReader reader = request.getReader();
String json = reader.readLine();
System.out.println(json);
reader.close();
if (!VoUtils.isNull(json)){
//将js串返回为java对象
config = mapper.readValue(json, ProductConfig.class);
}
java发送json
- servlet中相关代码
String jsonList = mapper.writeValueAsString(products);//转换成json 根据getxxx生成js对象
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonList);
js接收json
同发送,不再啰嗦
相关jar,js下载链接
相关项目连接