JSON(发送/接收)

来源:互联网 发布:淘宝商城女童套装 编辑:程序博客网 时间:2024/06/16 18:54

json

  1. 相关基础知识
    • json官网
    • json,w3c详情

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

同发送,不再啰嗦

  • 相关jar,js下载链接

    • 链接:http://pan.baidu.com/s/1hs1S4M4 密码:b8ww
  • 相关项目连接

    • 不知名项目
原创粉丝点击