最近在使用datatable,Struts2返回的JSON Datatable总是不能解析,通过Firebug看到返回Json的都带有反斜杠如下:
{\"Result\":\"OK\",\"Records\":[{\"PersonId\":1,\"Name\":\"Benjamin Button\",\"Age\":17,\"RecordDate\":\"\/Date(1320259705710)\/\"},{\"PersonId\":2,\"Name\":\"Douglas Adams\",\"Age\":42,\"RecordDate\":\"\/Date(1320259705710)\/\"},{\"PersonId\":3,\"Name\":\"Isaac Asimov\",\"Age\":26,\"RecordDate\":\"\/Date(1320259705710)\/\"}, {\"PersonId\":4,\"Name\":\"Thomas More\",\"Age\":65,\"RecordDate\":\"\/Date(1320259705710)\/\"}]}
原因:
Struts.xml
<result type="json"> <!-- result是action中设置的变量名,也是页面需要返回的数据,该变量必须有setter和getter方法 --> <param name="root">resultJSON</param> </result>
其中resultJSON我在Action中设置成了String类型,将其转换为net.sf.json.JSONObject即可