日期:2014-05-16  浏览次数:20529 次

jquery 传一个json对象到 java后台
前台

var data={};
data.fid="001";
data.fname="ok";
var myData=$.toJSON(data);



alert(myData);
打印的结果是:{"fid":"001","fname":"ok"}

然后使用jquery的ajax 
$.ajax({type:"post", url:"uWkTm.do?operate=insertPlan",
data:myData,



后台

BufferedReader br = new BufferedReader(new InputStreamReader(
                    (ServletInputStream) request.getInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            JSONObject jsonObj = JSONObject.fromObject(sb.toString());
            JSONObject objs = jsonObj.getJSONObject("data");//根据json格式里的 键 得到 值 (是个对象)
            UwkTmMain mainitem = (UwkTmMain) JSONObject.toBean(objs,
                    UwkTmMain.class);//强制转换为java对象