jquery遍歷json對象集合詳解
本文實(shí)例采用案例分析的方法介紹了jquery遍歷json對象的三種情況,供大家參考,具體內(nèi)容如下
第一個案例:jquery 遍歷json對象集合 常用示例
jsp中
$.ajax({
url: "${applicationScope.rootpath}common/getContractPage.html?userConId=${userConId}",
type: "post",
dataType:"json",
data: {},
success: function (jsonText) {
if(jsonText){
var status = jsonText.status;
var msg = jsonText.msg;
if(status == '500'){
//有異常的信息時
alert(msg);
}else{
$.each(jsonText,function(i,item){
var pngPath = item[0];
var pngName = item[1];
});
}
}
}
});
jsonText的格式:
{"status":"200","msg":[{"id":"1","name":"n1"},{"id":"2","name":"n2"}]}
{"status":"500","msg":"異常信息"}
java中:
List pngFileList = new ArrayList();//某對象集合
if(null != pngFileList && pngFileList.size() > 0) {
JSONArray pngFileArray =JSONArray.fromObject(pngFileList);
}
if(null != pngFileArray){
this.setTextAjax(pngFileArray.toString());
//異常的格式
//this.setTextAjax("{\"status\":\"500\",\"msg\":\""+errormsg+"\"}");//沒有記錄
/**
* ajax返回html,包括json形式
*
* @param responseContent
*/
public void setTextAjax(String responseContent) {
try {
HttpServletResponse response = getHttpResponse();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Content-Type", "text/html");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
PrintWriter out = response.getWriter();
out.print(responseContent);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
// ajaxResponse = new StringBufferInputStream(responseContent);
}
第二個案例:jQuery 遍歷JSON 對象
不說別的,直接貼代碼:
<script src="js/jquery-1.6.4.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#Link").click(function() {
var objson = "[{Title:'Sjr',Content:'Library',summary:'summary'},{Title:'Sjr',Content:'Library',summary:[{sum0:'sum0'},{sum0:'sum1'},{sum0:'sum2'}]},{Title:'Sjr',Content:'Library',summary:[{sum0:'sum0'},{sum0:'sum1'},{sum0:'sum2'}]}]";
var obj = eval(objson);
$(obj).each(function(index) {
var val = obj[index];
if (typeof (val.summary) == "object") {
$(val.summary).each(function(ind) {
alert(val.Title + " " + val.Content + " " + val.summary[ind].sum0);
});
} else {
alert(val.Title + " " + val.Content + " " + val.summary);
}
});
});
});
</script>
第三個案例:jquery中遍歷讀取json串中的對象
假設(shè)我們從服務(wù)器端獲取如下的json串,其中包括數(shù)組。我們該如何遍歷讀取呢?
使用.each難度太高,直接js讀取吧
//通過url獲取json對象
$.post("json/godjson!godlist", function (data){
//data.rows返回的是json字符串。需要轉(zhuǎn)換成json對象
var json = eval(data.rows)
//json變量現(xiàn)在就是一個數(shù)組對象,直接讀取每個數(shù)組對象。結(jié)合屬性進(jìn)行輸出
for(var i=0; i<json.length; i++){
alert(json[i].caishen+"--------- " + json[i].xishen);
}
//記得返回的數(shù)據(jù)類型一定要是json類型
}, "json");
通過如上的方式就能獲取json串的json對象并進(jìn)行輸出。
為大家分享這么多的案例,就是想幫助大家掌握jquery遍歷json對象集合的方法,真正理解,希望這篇文章可以幫助到大家。
- JS 遍歷 json 和 JQuery 遍歷json操作完整示例
- JS實(shí)現(xiàn)鍵值對遍歷json數(shù)組功能示例
- json的結(jié)構(gòu)與遍歷方法實(shí)例分析
- js遍歷json對象所有key及根據(jù)動態(tài)key獲取值的方法(必看)
- js遍歷json的key和value的實(shí)例
- JS動態(tài)遍歷json中所有鍵值對的方法(不知道屬性名的情況)
- 遍歷json 對象的屬性并且動態(tài)添加屬性的實(shí)現(xiàn)
- jquery對Json的各種遍歷方法總結(jié)(必看篇)
- jQuery遍歷json的方法分析
- JavaScript遍歷json對象數(shù)據(jù)的方法
相關(guān)文章
jQuery選取所有復(fù)選框被選中的值并用Ajax異步提交數(shù)據(jù)的實(shí)例
下面小編就為大家?guī)硪黄猨Query選取所有復(fù)選框被選中的值并用Ajax異步提交數(shù)據(jù)的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
ztree簡介_動力節(jié)點(diǎn)Java學(xué)院整理
zTree 是利用 JQuery 的核心代碼,實(shí)現(xiàn)一套能完成大部分常用功能的 Tree 插件,本文給大家簡單介紹下ztree的基本知識,感興趣的朋友一起看看吧2017-07-07

