java字符串返回成json



java端组装一个字符,返回json,json的数据不能用单引号,否则不无法返回成json

String children = "[{\"data\":\"child1\",\"attr\":\"11\"},{\"data\":\"child2\",\"attr\":\"12\"}]";
		


输出json
public void writeAjax(String data,HttpServletResponse response) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print(data);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.close();
		}
	}


最好不要自己组装json,使用JsonArray,JsonObject

你可能感兴趣的:(json,Ajax)