java web 接收json串

java web 接收json串_第1张图片
json
// 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();

你可能感兴趣的:(java web 接收json串)