$.post传JSON到后台及接收方法

日前同事问及我jquery 传送JSON数据到后台,后台该如何接收的问题,经多方查阅,总结出了以下方法:

前台要用到json2.js和jquery.js

后台要用到json.jar。

在前端要将传的JSON数据使用JSON.stringify(这里是json数据)转化成字符串,后台调用import net.sf.json.JSONObject方法或者采用获取字符流的方法来获取前台传送的数据。

例:

前端://拼装JSON格式数据json

             var json={"edit":[{"bh":"11111","mc":"11111","dbms":"ORACLE9i","dwmmc":"","addr":"","username":"","password":"","rm_sz":"ck","nyr_sz":"","id":"-1"},{"bh":"121","mc":"12","dbms":"ORACLE9i","dwmmc":"12","addr":"12","username":"12","password":"12","rm_sz":"ck","nyr_sz":"2011-09-06","id":"13"}],"del":["12"]};

             var post={data:JSON.stringify(json)};//JSON.stringify(json)把json转化成字符串
             $.post(url,post);

后台:JSONObject json=JSONObject.fromObject(request.getParameter("data"));
             List> edit=(List>) json.getJSONArray("edit");
             List del=(List) json.getJSONArray("del");

 

 

 

你可能感兴趣的:($.post传JSON到后台及接收方法)