ITeye博客

java字符串返回成json

hsys · 2011-07-13 12:34 · Ajax · Web前端 · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。