spring+json+jquery

Spring与JSON当输出中文有乱码时,可以直接在@RequestMapping(produces="application/json;charset=UTF-8")中加上produces即可解决。

我在使用@ResponseBody String返回JSON数据时老是给我报错:JSON.parse() expected property name or '}',搞了很久才找到原因,需要把单引号改成双引号就可以解决问题!

 1 @RequestMapping(value = "/home/getCategories", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
 2     public @ResponseBody String getCategories(HttpSession session) {
 3         User user = (User) session.getAttribute("user");
 4         Set<Category> catsSet = user.getCategories();
 5         StringBuilder sb = new StringBuilder();
 6         sb.append("[");
 7         int i = 0;
 8         for (Category cat : catsSet) {
 9             sb.append("{\"id\":" + cat.getId() + ",\"name\":\"" + cat.getName()
10                     + "\"}");
11             i++;
12             if (i < catsSet.size())
13                 sb.append(",");
14             
15             logger.info(cat.getName());
16         }
17         sb.append("]");
18         logger.info(sb.toString());
19         return sb.toString();
20     }

 

posted @ 2013-04-23 19:39  daveztong  阅读(381)  评论(0编辑  收藏  举报