requset 요청에서 json 데이터 가져 오기

이 기사에서는 모든 요청 매개 변수가 json에 배치되었을 때 요청 매개 변수를 얻는 방법을 간략하게 소개합니다 (requset.getparparameter를 가져올 수 없음).

요청 예제 :
여기에 사진 설명 삽입
솔루션 아이디어 : 스트림을 사용하여 문자열을 읽고 변환 한 다음 json 객체로 변환

public static String getPostString(HttpServletRequest request) {
    
    
		BufferedReader in = null;
		String parameters = "";
		try {
    
    
			in = new BufferedReader(new InputStreamReader(
					request.getInputStream(), "utf-8"));
			String ln;
			StringBuffer stringBuffer = new StringBuffer();
			while ((ln = in.readLine()) != null) {
    
    
				stringBuffer.append(ln);
				stringBuffer.append("\r\n");
			}
			parameters = stringBuffer.toString();
		} catch (UnsupportedEncodingException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}
		return parameters;
	}

//	实际应用
String reqBody = getPostString(request);
if(reqBody != null && !reqBody.equals("")) {
    
    
	String reqBodyinfo= URLDecoder.decode(reqBody, HTTP.UTF_8);
	JSONObject jsonData = (JSONObject) JSONValue.parse(reqBodyinfo);
	String data = jsonData.get("data")+"";
	// 如果data还是个json的话,就需要再转一下
	JSONObject json_data = (JSONObject) JSONValue.parse(data);
	//	再取其中的信息
	int gameID = Integer.parseInt(json_data.get("gameID")+"");
}

여기에 사진 설명 삽입

추천

출처blog.csdn.net/qq_42258975/article/details/108521074