springMVC中使用json格式传递数据

来源:互联网 发布:设计图片的软件 编辑:程序博客网 时间:2024/05/17 04:58
@RequestMapping("/getMenu")    public void getMenu(User user,HttpServletRequest request,HttpServletResponse response)    {        //String result = "[{'id': 1,'text': 'Node 1','state': 'closed','children': [{'id': 11,'text': 'Node 11'},{ 'id': 12,'text': 'Node 12'}]},{'id': 2,'text': 'Node 2','state': 'closed' }]";        //result 如果是上面的格式,ajax将不会接受到该数据,只能使用下面的格式        String result = "[{\"id\": \"1\",\"text\": \"Node 1\",\"state\": \"closed\",\"children\": [{\"id\": \"11\",\"text\": \"Node 11\"},{ \"id\": \"12\",\"text\": \"Node 12\"}]},{\"id\": \"2\",\"text\": \"Node 2\",\"state\": \"closed\" }]";        PrintWriter out = null;        response.setContentType("application/json"); //设置数据的格式为json                try {            out = response.getWriter();            out.write(result);            System.out.println("ok1");        } catch (IOException e) {            e.printStackTrace();        }    }

0 0
原创粉丝点击