JSON(发送/接收)

json

  1. 相关基础知识

js发送json串

  1. json2.js
  2. 列表内容
  3. 相关代码
 $(".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

同发送,不再啰嗦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值