java-web-接收json串

2022-07-15 13:36:38 浏览数 (1)

代码语言:javascript复制
// json测试 接收一个json串
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(
        new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));
String temp;
while ((temp = br.readLine()) != null) {
    sb.append(temp);
}
br.close();
System.out.println(sb.toString());




// 返回json数据
response.setContentType("application/x-javascript; charset=utf-8");  
PrintWriter pw = response.getWriter();  
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "aaa");
System.out.println(jsonObject.toString());
pw.write(jsonObject.toString());  
pw.flush();  
pw.close();

0 人点赞