jQuery處理json數(shù)據(jù)返回?cái)?shù)組和輸出的方法
更新時(shí)間:2015年03月11日 12:14:52 作者:iorichina
這篇文章主要介紹了jQuery處理json數(shù)據(jù)返回?cái)?shù)組和輸出的方法,涉及jQuery操作數(shù)組及json的技巧,需要的朋友可以參考下
本文實(shí)例講述了jQuery處理json數(shù)據(jù)返回?cái)?shù)組和輸出的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
/*print the json object
*
*$("selector").print_r_json(json,opts) : return formatted string (and print)
*sprint_r_json : just return the string;
*print_r_json : return the formatted string and print json data
*contribute 明河
*
*auth iorichina
*
*example:
*3 ways to use it
*<script language="javascript">
*$("selector").print_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"},{if_print:true,return_array:true});
*document.write($.sprint_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"}));
*$.print_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"});
*</script>
*
*/
$.fn.print_r_json = function(json,options){
if(typeof(json)!="object") return false;
var opts = $.extend({},$.fn.print_r_json.defaults,options);
var data = '';
if(opts.if_print)
{
data = $.sprint_r_json(json)
$(this).html('<div style="font-weight:bold">'+(opts.return_array?'Array':'JSON-DATA')+'</div>'+data);
}
if(opts.array)
{
return $.json_to_array(json);
}
return data;
};
$.fn.print_r_json.defaults =
{
if_print : false,//if print or just return formatted string
return_array : true //return an Array
};
$.extend({
print_r_json : function(json)
{
if(typeof(json)=="object")
{
var text='<div style="font-weight:bold;">{</div><div style="margin-left:25px;">';
document.write('<div style="font-weight:bold;">{</div><div style="margin-left:25px;">');
for(var p in json)
{
if(typeof(json[p])=="object")
{
document.write('<div>["'+p+'"] => ');
text+='<div>["'+p+'"] => '+$.print_r_json(json[p])+'</div>';
document.write('</div>');
}
else
{
text+='<div>['+((/^\d+$/).test(p)?p:('"'+p+'"'))+'] => "'+json[p]+'"</div>';
document.write('<div>['+p+'] => '+json[p]+'</div>');
}
}
text+='</div><div style="font-weight:bold;">}</div>';
document.write('</div><div style="font-weight:bold;">}</div>');
return (text);
}
else
{
document.write(json);
return (json);
}
},
sprint_r_json : function(json)
{
if(typeof(json)=="object")
{
var text = '<div style="font-weight:bold;">{</div><div style="margin-left:25px;">';
for(var p in json)
{
if(typeof(json[p])=="object")
{
text += '<div>["'+p+'"] => '+$.sprint_r_json(json[p])+'</div>';
}
else
{
text += '<div>['+((/^\d+$/).test(p)?p:('"'+p+'"'))+'] => "'+json[p]+'"</div>';
}
}
text += '</div><div style="font-weight:bold;">}</div>';
return (text);
}
else
{
return (json);
}
},
json_to_array : function(json)
{
if(typeof(json)=="object")
{
var text = new Array();
for(var p in json)
{
if(typeof(json[p])=="object")
{
text[p] = $.json_to_array(json[p]);
}
else
{
text[p] = json[p];
}
}
return (text);
}
else
{
return (json);
}
}
});
*
*$("selector").print_r_json(json,opts) : return formatted string (and print)
*sprint_r_json : just return the string;
*print_r_json : return the formatted string and print json data
*contribute 明河
*
*auth iorichina
*
*example:
*3 ways to use it
*<script language="javascript">
*$("selector").print_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"},{if_print:true,return_array:true});
*document.write($.sprint_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"}));
*$.print_r_json({"a":"aa","d":{"ef":{"a":"d","d":["a","b"]},"ed":"dd"},"g":"g"});
*</script>
*
*/
$.fn.print_r_json = function(json,options){
if(typeof(json)!="object") return false;
var opts = $.extend({},$.fn.print_r_json.defaults,options);
var data = '';
if(opts.if_print)
{
data = $.sprint_r_json(json)
$(this).html('<div style="font-weight:bold">'+(opts.return_array?'Array':'JSON-DATA')+'</div>'+data);
}
if(opts.array)
{
return $.json_to_array(json);
}
return data;
};
$.fn.print_r_json.defaults =
{
if_print : false,//if print or just return formatted string
return_array : true //return an Array
};
$.extend({
print_r_json : function(json)
{
if(typeof(json)=="object")
{
var text='<div style="font-weight:bold;">{</div><div style="margin-left:25px;">';
document.write('<div style="font-weight:bold;">{</div><div style="margin-left:25px;">');
for(var p in json)
{
if(typeof(json[p])=="object")
{
document.write('<div>["'+p+'"] => ');
text+='<div>["'+p+'"] => '+$.print_r_json(json[p])+'</div>';
document.write('</div>');
}
else
{
text+='<div>['+((/^\d+$/).test(p)?p:('"'+p+'"'))+'] => "'+json[p]+'"</div>';
document.write('<div>['+p+'] => '+json[p]+'</div>');
}
}
text+='</div><div style="font-weight:bold;">}</div>';
document.write('</div><div style="font-weight:bold;">}</div>');
return (text);
}
else
{
document.write(json);
return (json);
}
},
sprint_r_json : function(json)
{
if(typeof(json)=="object")
{
var text = '<div style="font-weight:bold;">{</div><div style="margin-left:25px;">';
for(var p in json)
{
if(typeof(json[p])=="object")
{
text += '<div>["'+p+'"] => '+$.sprint_r_json(json[p])+'</div>';
}
else
{
text += '<div>['+((/^\d+$/).test(p)?p:('"'+p+'"'))+'] => "'+json[p]+'"</div>';
}
}
text += '</div><div style="font-weight:bold;">}</div>';
return (text);
}
else
{
return (json);
}
},
json_to_array : function(json)
{
if(typeof(json)=="object")
{
var text = new Array();
for(var p in json)
{
if(typeof(json[p])=="object")
{
text[p] = $.json_to_array(json[p]);
}
else
{
text[p] = json[p];
}
}
return (text);
}
else
{
return (json);
}
}
});
希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- JS中Json數(shù)據(jù)的處理和解析JSON數(shù)據(jù)的方法詳解
- Java后臺(tái)處理Json格式數(shù)據(jù)的方法
- php處理json格式數(shù)據(jù)經(jīng)典案例總結(jié)
- JSON復(fù)雜數(shù)據(jù)處理之Json樹形結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)Java對(duì)象并存儲(chǔ)到數(shù)據(jù)庫的實(shí)現(xiàn)
- JavaScript處理解析JSON數(shù)據(jù)過程詳解
- jQuery中使用each處理json數(shù)據(jù)
- jquery處理json數(shù)據(jù)實(shí)例分析
- javascript:json數(shù)據(jù)的頁面綁定示例代碼
- jsp中將后臺(tái)傳遞過來的json格式的list數(shù)據(jù)綁定到下拉菜單select
- Json數(shù)據(jù)異步綁定到界面的Table并且自動(dòng)刷新原理及代碼
- JS 對(duì)java返回的json格式的數(shù)據(jù)處理方法
- json數(shù)據(jù)處理技巧(字段帶空格、增加字段、排序等等)
- json數(shù)據(jù)處理及數(shù)據(jù)綁定
相關(guān)文章
jQuery實(shí)現(xiàn)的鼠標(biāo)響應(yīng)緩沖動(dòng)畫效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的鼠標(biāo)響應(yīng)緩沖動(dòng)畫效果,涉及jQuery事件響應(yīng)、數(shù)值運(yùn)算及頁面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2018-02-02
jquery實(shí)現(xiàn)全選和全不選功能效果的實(shí)現(xiàn)代碼【推薦】
下面小編就為大家?guī)硪黄猨query實(shí)現(xiàn)全選和全不選功能效果的實(shí)現(xiàn)代碼【推薦】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05
JQuery Study Notes 學(xué)習(xí)筆記(一)
jquery是當(dāng)前比較流行的js類庫,學(xué)習(xí)它可以實(shí)現(xiàn)很多功能。2010-08-08
jQuery實(shí)現(xiàn)仿美橙互聯(lián)兩級(jí)導(dǎo)航菜單的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)仿美橙互聯(lián)兩級(jí)導(dǎo)航菜單的方法,實(shí)例分析了jQuery操作css及setTimeout等實(shí)現(xiàn)導(dǎo)航菜單的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
jQuery實(shí)現(xiàn)判斷上傳圖片類型和大小的方法示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)判斷上傳圖片類型和大小的方法,結(jié)合實(shí)例形式分析了jQuery針對(duì)上傳圖片屬性獲取、判定相關(guān)操作技巧,需要的朋友可以參考下2018-04-04

