Restful接口接收JSON字符串案例实战

   代码实战编写:

@RequestMapping(method=RequestMethod.POST,value="/uploadWeightDatas",params={"datas"})
        public void uploadWeightDatas(HttpServletRequest request,HttpServletResponse response)
    {
        String dataType1 = null;
        String deviceID1 = null;
        String collectDate1 = null;
        String dataValue = null;
        JSONObject result = new JSONObject();
        log.info("the begin of uploadWeightDatas...");
        String param = request.getParameter("datas");
        System.out.println("param="+param);
        log.info("data length = "+param.length());
        log.debug("datas:"+param);
        System.out.println(param);
        try {
            JSONObject map = JSONObject.fromObject(param);
            JSONArray ja = map.getJSONArray("data");
            System.out.println("ja.size = "+ja.size());
            
            for(int i=0;i<ja.size();i++)
            {
            System.out.println("ja="+ja);
            dataType1 = ja.getJSONObject(i).getString("dataType");
            if(dataType1 != null | dataType1.equals("dataType")){
            deviceID1 = ja.getJSONObject(i).getString("deviceID");
            collectDate1 = ja.getJSONObject(i).getString("collectDate");
            dataValue = ja.getJSONObject(i).getString("dataValue");
            JSONArray dv = ja.getJSONObject(i).getJSONArray("dataValue");
            
            System.out.println("dv="+dv);
            String mobile = null;
            System.out.println("dv.size="+dv.size());
            for(int j=0;j<dv.size();j++)
            {
                System.out.println(dv.getJSONObject(j));
                mobile = dv.getJSONObject(j).getString("mobile");
            }
    }
            }
}
        catch (Exception e) {
            log.error(e.getMessage());
    }
}

测试方法:

http://localhost:8080/DataService/uploadWeightDatas.json?datas={"data":[{"dataType":"weight","deviceID":"20110561","collectDate":"2016-07-16","dataValue":[{"mobile":"111111"}]}]}

用户火狐浏览器按照restClient插件,设置如下:

//header set : Content-Type:  application/x-www-form-urlencoded;text/plain;charset=UTF-8

测试结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值