java web 接收json串
2016-12-29 本文已影响57人
tea9
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();