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文件-->"[url]http://www.json.org/json.js[/url]"
可以在这里下载。继续看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 [[action]] Servlet.service() for servlet action threw exception
java.lang.UnsupportedClassVersionError: org/json/JSONException (Unsupported major.minor version 49.0)

原来是JAVA版本的问题。我用的1.4
今天从新看了下JSON jar包的问题。因为[url]http://www.json.org[/url]上面没有提供jar所以要自己打包,我在这位老兄je上下了个但是和源站的不同。。。都放上面来让大家吧 呵呵。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值