使用JSON格式传递数据,获取JSON的值

来源:互联网 发布:linux查看应用占用内存 编辑:程序博客网 时间:2024/06/06 20:36

项目中,接口之间传递数据是经常的,不管是项目内部接口传递数据,还是与外部系统之间接口传递数据,通过json都会较为容易一些,至于什么是json,自己goodle,百度吧。

在html中,使用ajax请求一个路径,请求方式为post,数据类型为json格式
上代码:

                    $.ajax({                            type : "POST",                            url : "./http_request/updateWtData.do",                            dataType : "json",                            //contentType : "application/json;charset=utf-8",                            data:{"data_json":json1},                            success : function(data) {                                if(data=="success"){                                    alert(data)                                }else{                                    alert(data)                                }                            }                        });

传递的数据类型为json格式,那json目前是有名称的(data_json),这样在后端,其实直接用“String str = request.getParameter(“data_json”);”就能获取到。
如果不知道json的名称,就麻烦了一下,如下面的代码。

上代码:

/**     * 获取json后更新库中数据的接口     *      * @param request     * @param name     * @return     */    @RequestMapping(value = "/updateWtData.do", method = RequestMethod.POST)    public @ResponseBody String updateWtData(HttpServletRequest request,            HttpServletResponse response) throws ServletException, IOException {        Map<String, String> map = new HashMap<String, String>()        /** String str = "{\"contracts\":{},\"goods\":\"goods_id\":1011364,\"goods_type\":30100,\"goods_name\":{\"2\":\"4G飞享套餐\"}}";**/        //如果不知道JSON的名称,需要从字节流里面取值        request.setCharacterEncoding("UTF-8");        BufferedReader br = new BufferedReader(new InputStreamReader( request.getInputStream(),"utf-8"));        String line = null;        StringBuilder sb = new StringBuilder();        while((line = br.readLine())!=null){            sb.append(line);        }        //得到一个字符串,转换成json格式即可        JSONObject a = JSONObject.fromObject(sb.toString());        //我的具体业务,可以不参考 start        map.put("goods_id",JSONObject.fromObject(a.get("goods")).get("goods_id").toString());        map.put("goods_name", (String)JSONObject.fromObject(JSONObject.fromObject(JSONObject.fromObject(a.get("goods")).get("goods_channel_tmp")).getJSONObject("goods_name")).values().toArray()[0]);        int r = 0;        Map<String,Object> count = httpRequestDao.countGoods(map);        if((long)(Long)count.get("count") < 1){            r = httpRequestDao.insertNewGoods(map);            System.out.println("insert***********");        }else{            r = httpRequestDao.updateGoods(map);            System.out.println("update***********");        }        //业务end        System.out.println(r);        return "200";    }

ok,其实如上代码, 会比没有json名称麻烦些,所以,在接口定义的时候还是要确定一下json的名称为好。

代码有点乱,不过关键的地方还是很清晰的,我把自己的业务代码加进来了,其实主要是在下面的代码中,注意的是要规定好编码,否则json会把有些符号当做空格的。

     request.setCharacterEncoding("UTF-8");        BufferedReader br = new BufferedReader(new InputStreamReader( request.getInputStream(),"utf-8"));        String line = null;        StringBuilder sb = new StringBuilder();        while((line = br.readLine())!=null){            sb.append(line);        }         //得到一个字符串,转换成json格式即可        JSONObject a = JSONObject.fromObject(sb.toString());

谢谢!

0 0
原创粉丝点击