参考了以下2个帖子
http://fhd001.iteye.com/blog/1136428
http://hi.baidu.com/lizhm/blog/item/a197e6116f8926d4a7ef3fa7.html
用的之前的工程。额外添加spring自动转JSON支持
导包
jackson-core-asl-1.5.6.jar jackson-mapper-asl-1.5.6.jar
spring配置文件 在之前的配置之上修改了
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name="messageConverters"> <util:list id="beanList"> <ref bean="json_demo"/> </util:list> </property> </bean> <bean id="json_demo" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
controller中 方法返回obj 并加上注解 @reponseBody 这样spring就能把对象给你转成json格式返回
@RequestMapping("/demo_graph.do") @ResponseBody public Object demo()throws Exception { String keyword_data = "[['Cup Holder Pinion Bob', 7], ['Generic Fog Lamp', 9], ['HDTV Receiver', 15],['8 Track Control Module', 12], [' Sludge Pump Fourier Modulator', 3],['Transcender/Spice Rack', 6], ['Hair Spray Danger Indicator', 18]]"; String keyword_title = "[1,2,4,5,3]"; String keyword_timeTitle = ""; Graph g = new Graph(); g.setName("keyword"); g.setGraph_data(keyword_data); g.setGraph_dateTime(keyword_timeTitle); g.setGraph_title(keyword_title); return g; }
$.ajax({ url : "demo_graph.do", dataTpye : "json", success : function(data) { alert(data.graph_title); } });