62,614
社区成员
发帖
与我相关
我的任务
分享
JSONArray json = JSONArray.fromObject(list);
resp.setCharacterEncoding("gbk");
PrintWriter pw = resp.getWriter();
pw.print(json);
<script type="text/javascript">
function loadJSON(){
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
var x;
var strText = "";
var jsonResult = eval("(" + xmlhttp.responseText + ")");
//var jsonResult = JSON.parse(xmlhttp.responseText);
for(x in jsonResult){
if(x == 0)
strText = jsonResult[x].firstName + ", " + jsonResult[x].lastName;
else
strText = strText + "; " + jsonResult[x].firstName + ", " + jsonResult[x].lastName;
}
alert(strText);
}
}
xmlhttp.open("post","写上实际的URL", true);
xmlhttp.send();
}
</script>
当然现代浏览器都提供了原生的 JSON 支持,你可以直接用JSON 解析器解析了,就是上面注释的代码;//要请求的一级机构JSON获取页面
$.getJSON("loadDistrictAndStreetServletAndTypes?method=getAllDistrict",function (data) {
//对请求返回的JSON格式进行分解加载
$(data).each(function () {
$("#district").append($("<option/>").text(this.name).attr("value",this.id));
});
});
$.ajax({
url: url地址,
data: {
参数1: 值1,
参数2: 值2
},
success: function(data) {
var array = eval('(' + data + ')');//处理后的json
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
}
});
jQuery.getJSON(url, function(data){console.log(data)});