欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通过HttpservletRequest接收json数据

程序员文章站 2024-02-04 20:36:04
...
	public String intercept(ActionInvocation invocation) throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();	  
        接收json数据
        BufferedReader streamReader = new BufferedReader( new 
        InputStreamReader(request.getInputStream(), "UTF-8"));
        StringBuilder responseStrBuilder = new StringBuilder();
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
             responseStrBuilder.append(inputStr);
         转化成json对象
        JSONObject jsonObject = JSONObject.fromObject(responseStrBuilder.toString());
        获取其中的对象
        JSONObject commonRequest=jsonObject.getJSONObject("commonRequest");
        获取对象中的字段
        String accessKey=commonRequest.getString("accessKey");
        获取其中的list数据
        List custList=(List) jsonObject.get("custList");
        获取的是字符串
        String  couponGroupId=jsonObject.getString("couponGroupId");
}
      

这是传的json数据 

{
    "commonRequest": {
    "accessKey": "1", 
    "tradeNo": "00010", 
    "tradeTime": "111111111", 
    "tradeMask": "7DC506B55F91677BE93A477D6EFF6C87"
    },
    "couponGroupId": "11", 
    "custList": [
        {
            "mobile": "15049995864", 
            "name": "zzz",
            "gender": "1", 
            "birthday": "2019-07-04", 
            "address": "zzzzzz",
            "areaCode": "1"
        }
    ]  
}