JSON问题
今天想跑通一下自己的AJAX+JSON+STRUTS1.2的程序。MyJson.jsp
<script src="Json/json.js"></script><script type="text/javascript" > var myjosn = { "people": [ {"name":"aaa","age":"10","birthday":"1000-01-01"}, {"name":"bbb","age":"20","birthday":"2000-02-02"}, {"name":"ccc","age":"30","birthday":"3000-03-03"} ] }; var xmlHttp = false; function aaa() { try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (e2) { xmlHttp = false;}}if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {xmlHttp = new XMLHttpRequest();} var url="<%=request.getContextPath()%>/ylsuccess.do?actionMethod=getJson&myJson="+escape(myjosn.toJSONString()); xmlHttp.open("POST",url,true); xmlHttp.onreadystatechange=getMessage; xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlHttp.send(null); } function getMessage() { if(xmlHttp.readystate==4) { if(xmlHttp.status==200) { alert(xmlHttp.readystate); } } }</script>
上面代码就是通过AJAX想Action发数据过去.但是遇到了一个问题。
toJSONString();<--这个方法我以为JS自带的方法。在网上找了很多例子都用这种方法。
可是我屡试不爽。终于发现要自己引入一个JS文件-->"http://www.json.org/json.js"
可以在这里下载。继续看ACTION:
AjaxTextAction.java:public ActionForward getJson(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {try {String myJson = (String)request.getParameter("myJson"); //获得传来的参数JSONObject json = new JSONObject(myJson);//转化为JSON对象JSONArray alist = json.getJSONArray("people");//获取对应的数组for (int i = 0; i < alist.length(); i++) {JSONObject jsono = (JSONObject) alist.get(i);//获取对应数组的每一个对象System.out.println(jsono.get("age"));System.out.println(jsono.get("name"));System.out.println(jsono.get("birthday"));}} catch (JSONException e) {e.printStackTrace();}return mapping.findForward("successJson");}
本以为这样可以轻松的解析JSON的数据。可是出现了下面的错误
11:36:04,423 ERROR [] Servlet.service() for servlet action threw exception
java.lang.UnsupportedClassVersionError: org/json/JSONException (Unsupported major.minor version 49.0)
原来是JAVA版本的问题。我用的1.4
今天从新看了下JSON jar包的问题。因为http://www.json.org上面没有提供jar所以要自己打包,我在这位老兄je上下了个但是和源站的不同。。。都放上面来让大家吧 呵呵。。。
页:
[1]